检测远程主机端口是否开通,并发邮件报警


[root@node230 ~]# cat mail.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
import sys
from email.mime.text import MIMEText
import time
#reload(sys)
#sys.setdefaultencoding('utf8')
current_time=time.strftime('%Y-%m-%d%H:%M',time.localtime(time.time()))
mail_host='mail.abc.com'
mail_user='abc@wsecyun.com'
mail_pwd='abc'
def send_email(mailto,get_sub,content):
    #msg = MIMEText( content.encode('utf8'),_subtype = 'html', _charset = 'utf8')
    msg = MIMEText(content,_subtype='plain',_charset='gb2312')
    msg['From']=mail_user
    msg['Subject']=get_sub
    #msg['To']=",".join(mailto)
    msg['To']=mailto
    try:
        s=smtplib.SMTP_SSL(mail_host,465)
        s.login(mail_user,mail_pwd)
        s.sendmail(mail_user,mailto,msg.as_string())
        s.close()
    except Exception as e:
        print 'Exception: ', e
    title=sys.argv[2]
    cont="""
    ---------------------------------
    摘要:  %s
    ---------------------------------
    时间:  %s
    ---------------------------------
    """%(sys.argv[3],current_time)
    to_list = ['%s' %(sys.argv[1]),]
    with open('sendmail_qs.log','ab') as f:
      f.write('%s  Send to  address:  %s Title: %s Substring: %s \n'%(current_time,sys.argv[1],title,sys.argv[3]))
if __name__ == "__main__":
    send_email(sys.argv[1], sys.argv[2], sys.argv[3])






[root@node230 ~]# cat check_remote_port.sh
#!/bin/bash
status=`nmap -p 19910 172.16.1.231 | grep "19910"|awk '{print $2}'`
#echo $status


if [ $status = 'closed' ];then
        echo "Remote Host 172.16.1.231 port 19910 is down"
        /usr/bin/python /root/mail.py rscpass@163.com  'Remote Host 172.16.1.231 port 19910 is down' 'Remote Host 172.16.1.231 port 19910 is down,Please check faster'
else
        echo "Remote Host 172.16.1.231 port 19910 is ok"
fi


加入定时任务:
crontab -l 

* * *  * * /bin/bash /root/check_remote_port.sh  
分割线
感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS