假设你有这样的东西(从这里复制):
#!/usr/bin/python
from scapy.all import *
TIMEOUT = 2
conf.verb = 0
for ip in range(0, 256):
packet = IP(dst="192.168.0." + str(ip), ttl=20)/ICMP()
reply = sr1(packet, timeout=TIMEOUT)
if not (reply is None):
print reply.src, "is online"
else:
print "Timeout waiting for %s" % packet[IP].src
在尝试下一个主机之前,无需等待每个 ping 完成。我可以将循环内部每次都按照 in 的线放入背景&
中:
for ip in 192.168.0.{0..255}; do
ping -c 1 $ip &
done