SHELL判断服务是不是正在运行

使用SHELL脚本进行检查服务开启情况

复制代码
#!/bin/bash
#需要首先安装 yum install nmap -y

#检查指定端口是否开启
function checkPortStatus()
{
        status=`nmap -sS 127.0.0.1 -p $1 | grep open | awk '{print $2}'`
        if [ "$status" != "open" ];
        then
                return 0;
        else
                return 1;
        fi
}

checkPortStatus 80
echo $?

checkPortStatus 81
echo $?
复制代码

 


调用python发送QQ邮件的邮件(可以防止垃圾邮件屏蔽)

复制代码
#-*-coding:utf-8-*-

#===============================================================================
# 导入smtplib和MIMEText
#===============================================================================
from email.MIMEText import MIMEText
from email.Header import Header
import smtplib, datetime,sys

#===============================================================================
# 要发给谁,这里发给1个人
#===============================================================================
mailto_list=["10402852@qq.com"]

#===============================================================================
# 设置服务器,用户名、口令以及邮箱的后缀
#===============================================================================
mail_host="smtp.qq.com"
mail_user="10402852"
mail_pass="*************"
mail_postfix="qq.com"

#===============================================================================
# 发送邮件
#===============================================================================
def send_mail(to_list,sub,content):
    '''
    to_list:发给谁
    sub:主题
    content:内容
    send_mail("10402852@qq.com","sub","content")
    '''
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content)
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user,mail_pass)
        s.sendmail(me, to_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False
if __name__ == '__main__':
    if send_mail(sys.argv[1],sys.argv[2],sys.argv[3]):
        print "发送成功"
    else:
        print "发送失败"
复制代码

 测试用例:

[root@199 huanghai]# python mail.py 10402852@qq.com 黄海的测试标题 黄海的测试内容
发送成功

 

watch.py 监控CPU,内存,磁盘等情况

复制代码
#!/usr/bin/python
#fileName:getinfoinsh.py
#get cpu,meminfo from top command.

import os
import time

def getinfointop():
    topp=os.popen("top -n1|grep -E '^Cpu|^Mem'")
    toppstr=topp.read()
    replacestr=["\x1b","[m","\x0f","[K"]
    # replace the str cannt be printed.
    for item in replacestr:toppstr=toppstr.replace(item,'')

    splitstr=toppstr.split("\n")

    cpuinfo=splitstr[0].split()
    meminfo=splitstr[1].split()
    info=(cpuinfo[1].strip(','),cpuinfo[2].strip(','),cpuinfo[4].strip(','),meminfo[3],meminfo[5],meminfo[1])
    return info


def getinfoindh():

    dhplines=[]
    for i in os.popen("df -h"):
        dhplines.append(i.strip())
    return dhplines


if __name__=='__main__':
    info=getinfointop()
    diskinfo=getinfoindh()
    print 'cpu info:'
    print "user cpu used:",info[0]
    print "system cpu used:",info[1]
    print "free cpu:",info[2]
    print ''
    print 'Mem info:'
    print "used mem:",info[3]
    print "free mem:",info[4]
    print "total mem:",info[5]
    print ''
    print 'disk info:'
    for i in diskinfo:print i
    print ''
    print 'time:', time.strftime('%Y-%m-%d  %H:%M',time.localtime(time.time()))
复制代码

 

posted @   糖豆爸爸  阅读(1974)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2013-07-02 Java组各任务工作流程
Live2D
点击右上角即可分享
微信分享提示