使用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 80echo $?checkPortStatus 81echo $?
调用python发送QQ邮件的邮件(可以防止垃圾邮件屏蔽)
#-*-coding:utf-8-*-#===============================================================================# 导入smtplib和MIMEText#===============================================================================from email.MIMEText import MIMETextfrom email.Header import Headerimport 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 Falseif __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 osimport timedef 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 infodef getinfoindh(): dhplines=[] for i in os.popen("df -h"): dhplines.append(i.strip()) return dhplinesif __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()))