码迷,mamicode.com
首页 > 编程语言 > 详细

python实现端口扫描监控,宕机则发送邮件

时间:2019-03-14 13:32:45      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:ext   rom   host   .com   net   sendmail   div   发送邮件   __name__   

 

 

import socket

def get_ip_status(ip,port):
    server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        server.connect((ip,port))
        print({0} port {1} is open.format(ip,port))
    except Exception as err:
        print({0} port {1} is down.format(ip,port))
        send_mail(ip,port)//宕机就发送邮件
    finally:
        server.close()
        
def send_mail(ip,port):
    fromAddr = "*********@126.com"
    toAddr = "*********@126.com"
    password = "************"
    smtp_server = "smtp.126.com"
    import smtplib
    from email.mime.text import MIMEText
    msg = MIMEText("Test body")  //处理邮件文本内容
    msg[Subject] = "the {0}:{1} is down".format(ip,port)
    //msg[‘Subject‘] = ‘%s %d is down!!!‘ %(ip,port)
    server = smtplib.SMTP(smtp_server,25)
    server.login(fromAddr,password)
    server.sendmail(fromAddr,toAddr,msg.as_string())
    
‘‘‘
if __name__ == __main__:
    host = 172.16.60.223
    port = [9000,9001,9002,9003,9008,9009]
    for p in port:
        get_ip_status(host,p)
        ‘‘‘
//多个ip不同端口扫描
if __name__ == __main__:
    host_port = {172.16.60.223:[9000,9001,9002,9003,9008,9009],172.16.60.40:[8003,8004,8005,8007,8011]}
    for h,p in host_port.items():
        for p in p:
            get_ip_status(h,p)

 

python实现端口扫描监控,宕机则发送邮件

标签:ext   rom   host   .com   net   sendmail   div   发送邮件   __name__   

原文地址:https://www.cnblogs.com/linyouyi/p/10529481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!