#導入模塊 import paramiko import smtplib from email.mime.text import MIMEText from email.header import Header ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) def send_mail(message): sender = 'aa1052995312@163.com' receiver = ['ruri9999@163.com'] subject = '報警' username = 'aa1052995312@163.com' password = '123456' msg = MIMEText(message, 'plain', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = 'warning<aa1052995312@163.com>' msg['To'] = "ruri9999@163.com" smtp = smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit() def server(): ssh.connect("192.168.88.31", 22, 'root', '123', timeout=3) #鏈接192.168.88.31 端口22 用戶root 密碼123 stdin, stdout, stderr = ssh.exec_command('./jk.sh ' ) #在linux中執行./jk.sh msg = stdout.read().decode('utf_8') war = "警告" while True: if war in msg: send_mail(msg) print('發送成功!!!') break else: print("server's find.") break server()