可以用来监控一些端口。
import socket def check_server(addr, port): s = socket.socket() try: s.connect((addr, port)) return True except socket.error: return False def alert(p): from email.mime.text import MIMEText import smtplib sender = "[email protected]" receiver = ["[email protected]"] host = ‘smtp.qq.com‘ passwd = ‘xxxxx‘ port = 465 content = ‘端口:%r 故障‘ % p msg = MIMEText(content) msg[‘From‘] = "[email protected]" msg[‘To‘] =‘;‘.join(receiver) msg[‘Subject‘] = "vps警报" try: smtp = smtplib.SMTP_SSL(host, port) smtp.login(sender, passwd) smtp.sendmail(sender, receiver, msg.as_string()) except Exception: print("error") if __name__==‘__main__‘: for p in [443, 2280]: result = check_server(‘23.188.2.75‘, p) if not result: alert(p)
时间: 2024-10-25 04:04:37