-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPing_thread.py
More file actions
83 lines (69 loc) · 2.13 KB
/
Ping_thread.py
File metadata and controls
83 lines (69 loc) · 2.13 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from multiprocessing import Pool
import threading, queue,os
import subprocess, time
in_queue=queue.Queue()
out_queue=queue.Queue()
def PING(iq,oq):
while True:
try:
ip=iq.get(block=False)
except queue.Empty:
break
try:
child=subprocess.check_output("{} {}".format("ping -n 1",ip),shell=True,universal_newlines=True)
if child.find('unreachable') >0:
pass
else:
child1=subprocess.Popen("{} {}".format("nbtstat -a",ip),shell=True,stdout=subprocess.PIPE)
child2=subprocess.Popen("find /i \"Registered\"",shell=True,universal_newlines=True, stdin=child1.stdout,stdout=subprocess.PIPE)
out=child2.communicate()[0]
oq.put("{} UP\n{}".format(ip,out))
except subprocess.CalledProcessError:
pass
iq.task_done()
# print("Run task {}({})".format(name,os.getpid()))
# start=time.time()
# time.sleep(random.random()*3)
# end=time.time()
# print("Task {} rans {:.02f} seconds".format(name,(end-start)))
if __name__=="__main__":
while True:
L=[]
IPSTART=str(input("Please input the start ip of ip range you want to check:"))
IPEND=str(input("Please input the end ip of the ip range you want to check:"))
# IPSTART='10.146.90.33'
# IPEND='10.146.90.40'
start=time.time()
NETWORK1=os.path.splitext(IPSTART)[0]
NETWORK2=os.path.splitext(IPEND)[0]
if NETWORK1 != NETWORK2:
input('{} and {} must have same network ip!!!'.format(IPSTART,IPEND))
exit()
START=int(os.path.splitext(IPSTART)[1].strip('.'))
END=int(os.path.splitext(IPEND)[1].strip('.'))+1
if START+1 > END:
input("{} must be greater than {}".format(IPEND,IPSTART))
exit()
IPS=[]
for i in range(START,END):
IPS.append("{}.{}".format(NETWORK1,i))
for ip in IPS:
in_queue.put(ip)
for i in range(30):
t=threading.Thread(target=PING,args=(in_queue,out_queue))
t.setDaemon(True)
t.start()
in_queue.join()
# out_queue.join()
t.join()
# L.append(out_queue.get())
# print(L)
# print(out_queue.empty())
while not out_queue.empty():
L.append(out_queue.get())
L=sorted(L,key=lambda nu: int(nu.split()[0].split('.')[-1]))
for i in L:
print(i)
end=time.time()
print("time: {:.02f}".format(end-start))
# input('END')