ping内网Ip,找出内网21网段未被使用的ip地址
#!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess import ipaddress import time def ping_ip(ip, timeout=1, retries=5, delay=1): """Ping an IP address multiple times with a timeout.""" success = False for _ in range(retries): try: # 使用subprocess执行Windows的ping命令,并设置超时 # 注意:Windows使用-n,Linux使用-c param = ['ping', '-n', '1', '-w', str(timeout * 1000), ip] # 注意:-w 的单位是毫秒 result = subprocess.run(param, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=(timeout + 2)) # 检查ping是否成功 # 通常,如果ping成功,result.stdout会包含"TTL="等字样 # 但更可靠的方式是检查returncode是否为0(可能需要根据你的环境调整) if "TTL=" in result.stdout: # 或者 if result.returncode == 0: success = True break else: print(f"Ping failed for {ip}: {result.stderr}") time.sleep(delay) # 等待一段时间后重试 except subprocess.TimeoutExpired: print(f"Ping timed out for {ip}") time.sleep(delay) except subprocess.CalledProcessError as e: # 当returncode不为0时,subprocess.run会抛出CalledProcessError # 但由于我们已经检查了stdout,这里可能不需要额外的处理 print(f"Ping failed with error code {e.returncode} for {ip}") break # 如果ping失败,可以选择不再重试 def find_unused_ips(network, prefix_len=24, ping_count=5): """Find unused IPs in a given network.""" net = ipaddress.ip_network(f"{network}/{prefix_len}", strict=False) unused_ips = [] # 跳过.1(通常是网关)和.255(广播地址) for host in net.hosts(): if host.is_private and not (host.exploded[-1] == '1' or host.exploded[-1] == '255'): if not ping_ip(str(host), ping_count): unused_ips.append(str(host)) return unused_ips # 假设我们要扫描的网段是192.168.21.x network = '192.168.21.0' unused_ips = find_unused_ips(network) print("Unused IPs in the network:", unused_ips)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?