A-A+
Windows服务器远程登录时 发送邮件提醒 的小工具
运行原理:利用计划任务,远程登陆后执行本工具,工具利用netstat获取mstsc的远程IP地址,发送邮件提醒
python源码:
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})
下载:
MailSend.7z (访问密码:1529)
下载:https://wws.lanzoui.com/ip6nYmpy6li 密码:53bs
链接:https://pan.baidu.com/s/1wmoD-9qHAlvg05EPeH1Q3A 提取码:m0it
复制这段内容后打开百度网盘手机App,操作更方便哦--来自百度网盘超级会员V2的分享