运行原理:
利用计划任务,远程登陆后执行本工具,工具利用netstat获取mstsc的远程IP地址,发送邮件提醒
提示效果:
计划任务设置方法:
配置文件参数说明:
成品下载:
https://590m.com/f/1259131-485138904-414593(访问密码:931103)Radmin远程伴侣:https://www.hxwglm.com/thread-38271-1-1.html
原代码:
- import configparser,zmail,getpass, os, socket, time,subprocess
- cfg = configparser.ConfigParser()
- cfg.read('Mail.ini', encoding="utf-8-sig")
- aa0,aa2,aa4= '
',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, '', '\n登录时间:\n',
- aa3, '\n', aa0, '\n''主机名:',
- aa2, '\n', aa0, '\n''用户名:',
- aa4, '\n', aa0, '\n', '登录IP地址:\n', '',
- '''\n',
- aa1, '' '\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})
复制代码 |