LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【Python源码】端口访问邮件提醒程序下载,可用于RDP和Radmin远程登录通知

admin
2023年3月1日 9:5 本文热度 371
工具介绍:
主要实现的功能是获取指定端口连接的远程 IP 地址,并将其发送给指定的邮箱,可用于一些远程工具的登录通知,或者其他你需要检测的程序。

特点:
1.  可在配置文件内自定义需要检测的端口;支持多端口检测;
2.  白名单功能,可设置多个IP排除;
3.  使用zmail模块发送邮件更高效方便,不需要手动添加服务器地址、端口以及适合的协议,zmail会帮你完成。

下载:

2023/3/1
添加了两条错误日志输出。

2023/2/23
使用python3.10编译,优化了一下代码。
执行过程:
1.使用 ConfigParser() 函数创建一个 config 实例,并使用 read() 方法读取名为 Mail.ini 的配置文件。然后获取 WL 段中的 add 选项的值并按逗号分隔成列表,将其保存到 wl_list 变量中。接着遍历 port 段中的所有选项,如果选项的名称以 net_port 开头且选项的值是数字,则将其转换为整数并调用 get_remote_ips() 函数获取当前连接到该端口的所有远程IP地址,将其保存到 remote_ips_dict 字典中。
2.定义函数 get_remote_ips(),它接收两个参数 port 和 wl_list,并使用 net_connections() 函数获取当前系统上所有建立连接的信息。然后使用生成器表达式筛选出所有远程IP地址,使得地址不是空值、状态为 ESTABLISHED、不是本地回环地址、不包含冒号,且本地地址的端口号等于 port 参数。然后使用列表推导式去重并过滤掉在 wl_list 中出现的地址,最终返回远程IP地址列表。
3.定义函数 send_mail(),它接收两个参数 remote_ips_dict 和 config。函数首先获取当前计算机的主机名、用户名和当前时间,并将其插入到邮件正文中。然后遍历 remote_ips_dict 字典中的所有端口及其对应的远程IP地址,如果有远程IP地址则将其插入到邮件正文中,最终将邮件正文和邮件标题通过 zmail 模块发送到指定的收件人。
4.如果 remote_ips_dict 字典中至少有一个非空列表,则调用 send_mail() 函数将所有远程IP地址发送到指定的收件人。

2023/2/20
有朋友希望能检测多个端口,便做了一下修改支持多端口。





python源码:

01
02
03
04
05
06
07
08
09
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
import psutil
import getpass
import time
import zmail
import configparser
import socket
 
 
def get_remote_ips(port, wl_list):
    remote_ips = [conn.raddr[0for conn in psutil.net_connections()
                  if conn.raddr and conn.status == 'ESTABLISHED'
                  and not conn.raddr[0].startswith('127.')
                  and ':' not in conn.raddr[0]
                  and conn.laddr[1== port
                  ]
    remote_ips = list(set(remote_ips))
 
    if not remote_ips or any(ip.startswith(wl) for ip in remote_ips for wl in wl_list if wl):
        return []
    return remote_ips
 
 
def send_mail(remote_ips, config):
    aa1, aa2 = socket.gethostname(), getpass.getuser()
    aa3 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
 
    ip_list = [ip + '\n' for ip in remote_ips]
    content = (
            '<font size="4">检测时间: {}<br>'.format(aa3)
            + '主机名: {}<br>'.format(aa1)
            + '用户名: {}<br>'.format(aa2)
            + '连接端口 "{}" 的IP地址:<br></font>'.format(port)
            + '<b><font color="#ff0000" size="5">{}</font></b><br>'.format('<br>'.join(ip_list))
            + '<a >查询IP归属地</a>'.format('<br>'.join(ip_list))
    )
 
    from_addr, pwd = config.get('Mail''from_addr'), config.get('Mail''pwd')
    title = config.get('Mail''title')
    to_addr = config.get('to_addr''add').split(',')
    server = zmail.server(from_addr, pwd)
    server.send_mail(to_addr, {'subject': title, 'content_html': content})
 
 
config = configparser.ConfigParser()
config.read('Mail.ini', encoding="utf-8-sig")
wl_list = config.get('WL''add').split(',')
port = int(config.get("port""net_port"))
 
remote_ips = get_remote_ips(port, wl_list)
if remote_ips:
    send_mail(remote_ips, config)

该文章在 2023/3/1 9:05:43 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved