我正在尝试编写一个 Python 脚本,它使用 Scapy 模块来 ping 内部 IP 范围以确定哪些 IP 在线。到目前为止我有这个:
#!/usr/bin/python
from scapy.all import *
conf.verb = 0
for ip in range(0, 256):
packet = IP(dst="192.168.0." + str(ip), ttl=20)/ICMP()
reply = sr1(packet)
if "192.168." in reply.src:
print reply.src, "is online"
并且程序会坐一会儿什么也不做,然后如果我用 CTRL+CI 杀死它,会收到一条错误消息:
Traceback (most recent call last):
File "sweep.py", line 7, in <module>
if "192.168." in reply.src:
AttributeError: 'NoneType' object has no attribute 'src'
但是,如果我尝试使用单个 IP 地址而不是范围,它可以工作。像这样:
#!/usr/bin/python
from scapy.all import *
conf.verb = 0
packet = IP(dst="192.168.0.195", ttl=20)/ICMP()
reply = sr1(packet)
if "192.168." in reply.src:
print reply.src, "is online"
有谁知道我该如何解决这个问题?或者您对如何使用 Scapy ping IP 范围以确定哪些主机在线有任何其他想法?