Scapy モジュールを使用して内部 IP 範囲に ping を実行し、どの IP がオンラインであるかを判断する Python スクリプトを作成しようとしています。私はこれまでのところこれを持っています:
#!/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 を使用して IP 範囲に ping を実行する方法について、他のアイデアはありますか?