windows服务器远程登录时自动发送邮件提醒工具
温馨提示:这篇文章已超过1368天没有更新,请注意相关的内容是否还可用!
运行原理:
利用计划任务,远程登陆后执行本工具,工具利用netstat获取mstsc的远程IP地址,发送邮件提醒
提示效果:
计划任务设置方法:
配置文件参数说明:
import configparser,zmail,getpass, os, socket, time,subprocess cfg = configparser.ConfigParser() cfg.read('Mail.ini', encoding="utf-8-sig") aa0,aa2,aa4= '<br>',socket.gethostname(),getpass.getuser() aa3=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) port,CREATE_NO_WINDOW=cfg.get('port', 'net_port'),0x08000000 proc = subprocess.Popen("netstat -n", stdout = subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, creationflags=CREATE_NO_WINDOW) try: outs, errs = proc.communicate(timeout=15) except TimeoutExpired: proc.kill() outs, errs = proc.communicate() for n in outs.decode("GBk").split("\n"): if n.find(port) > -1: aa1= (n.split(":")[1].split(" ")[-1]) with open('Login_info_send.log', 'w+', encoding="gbk") as f: f.writelines([ aa0, '<font size="4">', '\n登录时间:\n', aa3, '\n', aa0, '\n''主机名:', aa2, '\n', aa0, '\n''用户名:', aa4, '\n', aa0, '\n', '登录IP地址:\n', '</font>', '<b><font color="#ff0000" size="5">''\n', aa1, '</font></b>' '\n']) f.seek(0) content = f.read() # 读取邮件发送参数 from_addr,pwd= cfg.get('Mail', 'from_addr'),cfg.get('Mail', 'pwd') title = cfg.get('Mail','title') to_addr = cfg.get('to_addr','add').split(',') server = zmail.server(from_addr,pwd) server.send_mail(to_addr,{'subject':title,'content_html':content})
免责声明:本文来自柒叁,不代表云维护的观点和立场,如有侵权请联系本平台处理。
还没有评论,来说两句吧...