假設你有這樣的事情(從here複製):加快Python腳本的for循環
#!/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來嘗試下一個主機之前完成。我可以每次把循環內部爲沿&
中的行背景:
for ip in 192.168.0.{0..255}; do
ping -c 1 $ip &
done
跑題了,但你知道,'範圍(0,255)''在結束254'? –
感謝您的錯字更正 – user857990
您可以使用['concurrent.futures'](http://docs.python.org/3/library/concurrent.futures.html#module-concurrent.futures)模塊同時執行ping命令。 –