邮箱准备 1.注册126邮箱
2.邮箱设置POP3/SMTP服务
3.保存授权码
python脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 import socketimport osimport smtplibfrom email.mime.text import MIMETextfrom email.header import Headerdef send_email (new_ip ): smtp = smtplib.SMTP() smtp.connect("smtp.126.com" , port=25 ) smtp.login(user="monitor_lzh@126.com" , password="*****保存的授权码*****" ) message = MIMEText('P330工作站 IP变更为' + new_ip, 'plain' , 'utf-8' ) message['From' ] = Header("P330 IP变更监控" , 'utf-8' ) message['To' ] = Header("Users" , 'utf-8' ) message['Subject' ] = Header('P330工作站IP变更 ' + new_ip, 'utf-8' ) smtp.sendmail(from_addr="monitor_lzh@126.com" , to_addrs="指定收件邮箱" , msg=message.as_string()) if os.path.exists('./email.txt' ): fr = open ('./email.txt' ,'r' ,encoding='utf-8' ) emails = fr.readline().strip('\n' ).split('\n' ) if emails: for email in emails: if email.strip() != '' and email.strip() != '\n' and '@' in email: smtp.sendmail(from_addr="monitor_lzh@126.com" , to_addrs=email, msg=message.as_string()) def main (): log_path = './ip_log.log' fa = open (log_path, 'a' , encoding='utf-8' ) cur_ip = socket.gethostbyname(socket.gethostname()) if not os.path.isfile(log_path) and fa.read().strip() == '' : fa.write(cur_ip + '##' ) send_email(cur_ip) fa.close() return 0 with open (log_path, 'r' , encoding='utf-8' ) as fr: old_ip = fr.read().strip('##' ).split('##' )[-1 ] if old_ip != cur_ip or old_ip is None : fa.write(cur_ip + '##' ) send_email(cur_ip) fa.close() if __name__ == '__main__' : main()
注:在该同目录新建一个email.txt
可以给多个邮箱发送邮件,一行一个邮箱地址,不留空格
打包成exe程序 安装pyinstaller模块
在相应py脚本目录下打开命令行执行pyinstaller -F ./get_ip.py
对应的exe文件在dist文件夹中
配置任务计划 win+r
输入 taskschd.msc
打开任务计划窗口,根据需要创建基本任务,指定到exe程序,关键点部分我已截图标注
设定程序每5分钟执行一次即可,5分钟的间隔设置可以在创建完任务后选中该任务,打开属性→触发器
进行设置
然后可以右键指定任务点击运行
跑一次看看