扫描IP段内的使用的IP
方法一:命令行
命令行进入
bash
for /L %i IN (1,1,254) DO ping -w 1 -n 1 192.168.3.%i
bash
arp -a
方法二:python
python
from scapy.all import ARP, Ether, srp
import keyboard
def scan_network(ip_range):
# 创建一个ARP请求包
arp = ARP(pdst=ip_range)
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
packet = ether / arp
# 发送包并接收响应
result = srp(packet, timeout=2, verbose=0)[0]
# 解析响应
devices = []
for sent, received in result:
devices.append({'ip': received.psrc, 'mac': received.hwsrc})
return devices
if __name__ == "__main__":
# 指定要扫描的网段
target_ip_range = str(input("请输入IP(格式:192.168.20.0/23):"))
# target_ip_range = "192.168.20.0/23"
active_devices = scan_network(target_ip_range)
print("Active devices in the network:")
for device in active_devices:
print(f"IP: {device['ip']}, MAC: {device['mac']}")
print("按空格键退出")
# 检查是否有按键被按下
while True:
if keyboard.is_pressed('space'):
print("Exiting...")
break
可以用pyinstaller打包成exe来处理,这个比方法一会快一点。
还有优化空间,因为目前是单线程来的