-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_all_ip.py
61 lines (48 loc) · 1.17 KB
/
ping_all_ip.py
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
58
59
60
61
#!/usr/bin/env python
from threading import Thread
import subprocess
from Queue import Queue
import socket
dlb_debug=0
num_threads=10
ip_start=1
ip_end=254
ip_prefix='10.204.5'
ips=[]
for i in range(ip_start, ip_end + 1):
ips.append('%s.%s' %(ip_prefix, i))
report=[{} for row in range(num_threads)]
q=Queue()
def pingip(i, queue):
while True:
ip=queue.get()
if dlb_debug!=0:
print 'Thread %s pinging %s' %(i, ip)
p=subprocess.Popen('ping -c 4 %s' % ip
,shell=True
,stdout=open('/dev/null','w')
,stderr=subprocess.STDOUT)
ret=p.wait()
if ret==0:
report[i]['%s' %ip]='is alive!'
else:
report[i]['%s' %ip]='is down...'
queue.task_done()
# Branch num_threads threads
for i in range(num_threads):
t=Thread(target=pingip, args=(i, q))
t.setDaemon(True)
t.start()
# Main thread
for ip in ips:
q.put(ip)
print 'Going to ping %s.1 ~ %s' %(ip_prefix, ip_end)
print 'Main thread waiting ...'
q.join();print 'Done'
# Print report
buf={}
for i in range(num_threads):
buf.update(report[i])
# Sort the reports by IP
for k in sorted(buf.iterkeys(), key=socket.inet_aton):
print k, buf[k]