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)
原文地址:https://www.cnblogs.com/linyouyi/p/10529481.html
时间: 2024-10-26 13:59:59