fastping

一个快速ping某个C段存活ip的tool。

1
2
3
4
5
6
7
8
$ python fastping.py
Usage: fastping.py net [start] [end]
$ sudo python fastping.py 200
192.168.200.2
192.168.200.3
192.168.200.5
192.168.200.6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import threading
import Queue
import subprocess
import os
import sys
CONCURRENT = 100
def pinger(ip):
with open(os.devnull, 'w') as dev_null:
try:
subprocess.check_call(['ping', '-c1', ip], stdout=dev_null)
except:
return False
else:
return True
def ping(jobs, result):
ip = jobs.get()
if pinger(ip):
result.put(ip)
def fastping(net, start, end):
jobs = Queue.Queue()
result = Queue.Queue()
thread_list = []
for i in range(start, end):
ip = '192.168.%d.%d' % (net, i)
jobs.put(ip)
thread = threading.Thread(target=ping, args=(jobs, result))
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
result_list = []
while not result.empty():
result_list.append(result.get())
return result_list
if __name__ == '__main__':
if len(sys.argv) > 4 or len(sys.argv) < 2:
print 'Usage: %s net [start] [end]' % sys.argv[0]
exit(1)
start = sys.argv[2] if len(sys.argv) == 3 else 1
end = sys.argv[3] if len(sys.argv) == 4 else 254
net = sys.argv[1]
start, end, net = map(int, [start, end, net])
result = fastping(net, start, end)
for ip in sorted(result, key=lambda x: int(x.split('.')[3])):
print ip
如果您觉得这篇文章对您有帮助,不妨支持我一下!