各位运维兄弟,可能平时也都会用ping命令巡检设备的在线情况,如果设备数量比较多的情况下,不可能一个一个的ping,这样也比较麻烦,效率也比较差,我们一直也有这个困扰,最后借助AI的帮助,写了一个测试所有设备是否在线python程序,可以生成excel和文本文档,并能发送到邮箱,这样的话就节省好多时间,咋也不多说了,直接上代码了:
import platformimport subprocessimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.application import MIMEApplicationimport osfrom concurrent.futures import ThreadPoolExecutorimport datetimeimport pandas as pd# ===================== 内网网段配置 =====================NETWORKS = ["10.0.0","192.168.0","192.168.1","192.168.2","192.168.3","192.168.4","192.168.5"]IP_START = 1IP_END = 254MAX_WORKERS = 100# ===================== 邮箱配置 =====================MAIL_SENDER = "28666667@qq.com"MAIL_PASSWORD = "33333"MAIL_RECEIVER = "555552@qq.com"MAIL_SERVER = "smtp.qq.com"MAIL_PORT = 465# ====================== ping 单个IP ======================def ping_ip(ip): param = "-n" if platform.system().lower() == "windows" else "-c"command = ["ping", param, "2", "-w", "300", ip]try: output = subprocess.call(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)return ip, output == 0except:return ip, False# ====================== 扫描网段 ======================def scan_network(network):print(f"\n==== 正在扫描网段:{network}.0/24 ====") ips = [f"{network}.{i}" for i in range(IP_START, IP_END + 1)] online_ips = [] offline_ips = []with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: results = executor.map(ping_ip, ips)for ip, is_online in results:if is_online: online_ips.append(ip)print(f"✅ {ip} 在线")else: offline_ips.append(ip)return network, online_ips, offline_ips# ====================== 导出在线IP到Excel ======================def export_online_to_excel(online_ips, save_time): df = pd.DataFrame({"序号": range(1, len(online_ips) + 1),"在线IP地址": online_ips }) excel_filename = f"在线IP列表_{save_time}.xlsx"df.to_excel(excel_filename, sheet_name="在线IP", index=False)print(f"📊 Excel 已保存:{excel_filename}")return excel_filename# ====================== 发送邮件(100% 不会变成 bin) ======================def send_email(content, excel_file):try: msg = MIMEMultipart() msg["From"] = MAIL_SENDER msg["To"] = MAIL_RECEIVER msg["Subject"] = "内网IP扫描报告(Excel附件)"# 正文msg.attach(MIMEText(content, "plain", "utf-8"))# ========== 核心修复:QQ邮箱专用,绝对不会变成 bin ==========with open(excel_file, "rb") as f: part = MIMEApplication(f.read(), Name=os.path.basename(excel_file)) part["Content-Disposition"] = f'attachment; filename="{os.path.basename(excel_file)}"'msg.attach(part)# 发送with smtplib.SMTP_SSL(MAIL_SERVER, MAIL_PORT) as server: server.login(MAIL_SENDER, MAIL_PASSWORD) server.sendmail(MAIL_SENDER, [MAIL_RECEIVER], msg.as_string())print("✅ 邮件发送成功!")except smtplib.SMTPAuthenticationError:print("❌ 授权码错误!")except Exception as e:print(f"❌ 邮件发送失败:{e}")# ====================== 主程序 ======================def main(): start_time = datetime.datetime.now() time_str = start_time.strftime('%Y%m%d_%H%M%S')print("=" * 60)print(f"内网IP通断检测工具 | 开始时间:{start_time.strftime('%Y-%m-%d %H:%M:%S')}")print("=" * 60) all_online = [] all_offline = []for net in NETWORKS: network, online, offline = scan_network(net) all_online.extend(online) all_offline.extend(offline)print("\n" + "=" * 60)print("==== 扫描结果汇总 ====")print(f"在线 IP 总数:{len(all_online)}")print(f"离线 IP 总数:{len(all_offline)}")print("=" * 60) content = f"内网IP扫描报告\n生成时间:{start_time.strftime('%Y-%m-%d %H:%M:%S')}\n\n"content += f"在线IP({len(all_online)}个):\n"content += "\n".join(all_online) + "\n\n"content += f"离线IP({len(all_offline)}个):\n"content += "\n".join(all_offline)# 导出Excelexcel_filename = export_online_to_excel(all_online, time_str)# 发邮件send_email(content, excel_filename)# 保存TXTfilename = f"内网IP扫描报告_{time_str}.txt"with open(filename, "w", encoding="utf-8") as f: f.write(content)print(f"📄 报告已保存:{filename}")print("\n🎉 扫描完成!")if __name__ == "__main__": main()D:\python\pythonProject3\.venv\Scripts\python.exe D:\python\pythonProject3\检测监控网络设备是否在线.py
============================================================
内网IP通断检测工具 | 开始时间:2026-03-31 07:48:56
============================================================
==== 正在扫描网段:10.0.0.0/24 ====
✅ 10.0.0.5 在线
✅ 10.0.0.6 在线
✅ 10.0.0.7 在线
✅ 10.0.0.8 在线
✅ 10.0.0.9 在线
✅ 10.0.0.10 在线
✅ 10.0.0.11 在线
✅ 10.0.0.12 在线
✅ 10.0.0.15 在线
✅ 10.0.0.16 在线
✅ 10.0.0.17 在线
✅ 10.0.0.18 在线
✅ 10.0.0.19 在线
✅ 10.0.0.20 在线
✅ 10.0.0.21 在线
✅ 10.0.0.22 在线
✅ 10.0.0.24 在线
✅ 10.0.0.25 在线
✅ 10.0.0.27 在线
✅ 10.0.0.29 在线
✅ 10.0.0.54 在线
✅ 10.0.0.59 在线
✅ 10.0.0.61 在线
✅ 10.0.0.63 在线
✅ 10.0.0.87 在线
✅ 10.0.0.131 在线
✅ 10.0.0.167 在线
✅ 10.0.0.200 在线
✅ 10.0.0.250 在线
✅ 10.0.0.253 在线
==== 正在扫描网段:192.168.0.0/24 ====
✅ 192.168.0.1 在线
✅ 192.168.0.2 在线
✅ 192.168.0.3 在线
✅ 192.168.0.5 在线
✅ 192.168.0.6 在线
✅ 192.168.0.8 在线
✅ 192.168.0.10 在线
✅ 192.168.0.17 在线
✅ 192.168.0.18 在线
✅ 192.168.0.19 在线
✅ 192.168.0.25 在线
✅ 192.168.0.29 在线
✅ 192.168.0.32 在线
✅ 192.168.0.33 在线
✅ 192.168.0.35 在线
✅ 192.168.0.36 在线
✅ 192.168.0.39 在线
✅ 192.168.0.41 在线
✅ 192.168.0.44 在线
✅ 192.168.0.45 在线
✅ 192.168.0.48 在线
✅ 192.168.0.53 在线
✅ 192.168.0.60 在线
✅ 192.168.0.61 在线
✅ 192.168.0.62 在线
✅ 192.168.0.65 在线
✅ 192.168.0.66 在线
✅ 192.168.0.67 在线
✅ 192.168.0.69 在线
✅ 192.168.0.70 在线
✅ 192.168.0.71 在线
✅ 192.168.0.72 在线
✅ 192.168.0.82 在线
✅ 192.168.0.83 在线
✅ 192.168.0.85 在线
✅ 192.168.0.89 在线
✅ 192.168.0.92 在线
✅ 192.168.0.93 在线
✅ 192.168.0.95 在线
✅ 192.168.0.96 在线
✅ 192.168.0.97 在线
✅ 192.168.0.99 在线
✅ 192.168.0.100 在线
✅ 192.168.0.102 在线
✅ 192.168.0.103 在线
✅ 192.168.0.107 在线
✅ 192.168.0.110 在线
✅ 192.168.0.111 在线
✅ 192.168.0.112 在线
✅ 192.168.0.114 在线
✅ 192.168.0.115 在线
✅ 192.168.0.116 在线
✅ 192.168.0.118 在线
✅ 192.168.0.119 在线
✅ 192.168.0.121 在线
✅ 192.168.0.122 在线
✅ 192.168.0.123 在线
✅ 192.168.0.126 在线
✅ 192.168.0.131 在线
✅ 192.168.0.136 在线
✅ 192.168.0.137 在线
✅ 192.168.0.141 在线
✅ 192.168.0.142 在线
✅ 192.168.0.143 在线
✅ 192.168.0.146 在线
✅ 192.168.0.148 在线
✅ 192.168.0.154 在线
✅ 192.168.0.155 在线
✅ 192.168.0.156 在线
✅ 192.168.0.158 在线
✅ 192.168.0.159 在线
✅ 192.168.0.161 在线
✅ 192.168.0.166 在线
✅ 192.168.0.168 在线
✅ 192.168.0.169 在线
✅ 192.168.0.171 在线
✅ 192.168.0.172 在线
✅ 192.168.0.174 在线
✅ 192.168.0.175 在线
✅ 192.168.0.176 在线
✅ 192.168.0.177 在线
✅ 192.168.0.179 在线
✅ 192.168.0.184 在线
✅ 192.168.0.191 在线
✅ 192.168.0.194 在线
✅ 192.168.0.198 在线
✅ 192.168.0.199 在线
✅ 192.168.0.200 在线
✅ 192.168.0.202 在线
✅ 192.168.0.206 在线
✅ 192.168.0.207 在线
✅ 192.168.0.209 在线
✅ 192.168.0.213 在线
✅ 192.168.0.214 在线
✅ 192.168.0.216 在线
✅ 192.168.0.217 在线
✅ 192.168.0.218 在线
✅ 192.168.0.219 在线
✅ 192.168.0.228 在线
✅ 192.168.0.229 在线
✅ 192.168.0.231 在线
✅ 192.168.0.232 在线
✅ 192.168.0.233 在线
✅ 192.168.0.235 在线
✅ 192.168.0.238 在线
✅ 192.168.0.239 在线
✅ 192.168.0.241 在线
✅ 192.168.0.242 在线
✅ 192.168.0.243 在线
✅ 192.168.0.244 在线
✅ 192.168.0.245 在线
✅ 192.168.0.250 在线
✅ 192.168.0.251 在线
==== 正在扫描网段:192.168.1.0/24 ====
✅ 192.168.1.1 在线
✅ 192.168.1.7 在线
✅ 192.168.1.8 在线
✅ 192.168.1.10 在线
✅ 192.168.1.12 在线
✅ 192.168.1.13 在线
✅ 192.168.1.16 在线
✅ 192.168.1.17 在线
✅ 192.168.1.24 在线
✅ 192.168.1.33 在线
✅ 192.168.1.42 在线
✅ 192.168.1.44 在线
✅ 192.168.1.45 在线
✅ 192.168.1.46 在线
✅ 192.168.1.48 在线
✅ 192.168.1.63 在线
✅ 192.168.1.78 在线
✅ 192.168.1.81 在线
✅ 192.168.1.83 在线
✅ 192.168.1.88 在线
✅ 192.168.1.89 在线
✅ 192.168.1.90 在线
✅ 192.168.1.95 在线
✅ 192.168.1.100 在线
✅ 192.168.1.101 在线
✅ 192.168.1.102 在线
✅ 192.168.1.103 在线
✅ 192.168.1.110 在线
✅ 192.168.1.117 在线
✅ 192.168.1.119 在线
✅ 192.168.1.120 在线
✅ 192.168.1.121 在线
✅ 192.168.1.125 在线
✅ 192.168.1.143 在线
✅ 192.168.1.145 在线
✅ 192.168.1.157 在线
✅ 192.168.1.158 在线
✅ 192.168.1.160 在线
✅ 192.168.1.161 在线
✅ 192.168.1.162 在线
✅ 192.168.1.163 在线
✅ 192.168.1.164 在线
✅ 192.168.1.165 在线
✅ 192.168.1.166 在线
✅ 192.168.1.167 在线
✅ 192.168.1.168 在线
✅ 192.168.1.169 在线
✅ 192.168.1.174 在线
✅ 192.168.1.176 在线
✅ 192.168.1.177 在线
✅ 192.168.1.179 在线
✅ 192.168.1.180 在线
✅ 192.168.1.181 在线
✅ 192.168.1.185 在线
✅ 192.168.1.186 在线
✅ 192.168.1.187 在线
✅ 192.168.1.188 在线
✅ 192.168.1.189 在线
✅ 192.168.1.191 在线
✅ 192.168.1.192 在线
✅ 192.168.1.195 在线
✅ 192.168.1.196 在线
✅ 192.168.1.198 在线
✅ 192.168.1.201 在线
✅ 192.168.1.206 在线
✅ 192.168.1.208 在线
✅ 192.168.1.209 在线
✅ 192.168.1.214 在线
✅ 192.168.1.217 在线
✅ 192.168.1.220 在线
✅ 192.168.1.221 在线
✅ 192.168.1.222 在线
✅ 192.168.1.223 在线
✅ 192.168.1.224 在线
✅ 192.168.1.241 在线
==== 正在扫描网段:192.168.2.0/24 ====
✅ 192.168.2.1 在线
✅ 192.168.2.2 在线
✅ 192.168.2.4 在线
✅ 192.168.2.5 在线
✅ 192.168.2.6 在线
✅ 192.168.2.8 在线
✅ 192.168.2.10 在线
✅ 192.168.2.11 在线
✅ 192.168.2.12 在线
✅ 192.168.2.13 在线
✅ 192.168.2.14 在线
✅ 192.168.2.15 在线
✅ 192.168.2.16 在线
✅ 192.168.2.17 在线
✅ 192.168.2.18 在线
✅ 192.168.2.19 在线
✅ 192.168.2.20 在线
✅ 192.168.2.22 在线
✅ 192.168.2.23 在线
✅ 192.168.2.24 在线
✅ 192.168.2.25 在线
✅ 192.168.2.26 在线
✅ 192.168.2.52 在线
✅ 192.168.2.234 在线
==== 正在扫描网段:192.168.3.0/24 ====
✅ 192.168.3.1 在线
✅ 192.168.3.100 在线
✅ 192.168.3.253 在线
==== 正在扫描网段:192.168.4.0/24 ====
✅ 192.168.4.1 在线
✅ 192.168.4.44 在线
✅ 192.168.4.49 在线
✅ 192.168.4.62 在线
✅ 192.168.4.65 在线
✅ 192.168.4.68 在线
✅ 192.168.4.100 在线
✅ 192.168.4.101 在线
✅ 192.168.4.102 在线
✅ 192.168.4.254 在线
==== 正在扫描网段:192.168.5.0/24 ====
✅ 192.168.5.1 在线
============================================================
==== 扫描结果汇总 ====
在线 IP 总数:256
离线 IP 总数:1522
============================================================
📊 Excel 已保存:在线IP列表_20260331_074856.xlsx
✅ 邮件发送成功!
📄 报告已保存:内网IP扫描报告_20260331_074856.txt
🎉 扫描完成!
进程已结束,退出代码为 0
最后,我们看下邮箱:
已经有了,今天就测试到这里,不过要注意,设备阻止ping的话肯定就不通了。