-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpscan_multithread.py
executable file
·85 lines (71 loc) · 1.75 KB
/
tcpscan_multithread.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#! /usr/bin/env python
import os
import collections
import platform
import socket, subprocess,sys
import threading
from datetime import datetime
''' section 1 '''
net = raw_input("Enter the Network Address: ")
net1= net.split('.')
a = '.'
net2 = net1[0]+a+net1[1]+a+net1[2]+a
st1 = int(raw_input("Enter the Starting Number: "))
en1 = int(raw_input("Enter the Last Number: "))
en1 =en1+1
dic = collections.OrderedDict()
oper = platform.system()
if oper == 'Window':
ping1 = "ping -n 1 "
elif (oper== "Linux"):
ping1 = "ping -c 1 "
else :
ping1 = "ping -c 1 "
t1=datetime.now()
'''section 2'''
class myThread(threading.Thread):
def __init__(self, st, en):
threading.Thread.__init__(self)
self.st = st
self.en = en
def run(self):
run1(self.st, self.en)
'''section3'''
def run1(st1, en1):
#print "scanning in progress"
for ip in xrange(st1, en1):
addr = net2 + str(ip)
comm = ping1 + addr
response = os.popen(comm)
for line in response.readlines():
if line.count('ttl'):
break
if line.count('ttl'):
dict[ip] = addr
'''section 4'''
total_ip = en1-st1
tn=20 #number of ip handled by one thread
total_thread = total_ip/tn
total_thread +=1
threads = []
try:
for i in xrange(total_thread):
en = st1+tn
if (en > en1):
en = en1
thread = myThread(st1,en)
thread.start()
threads.append(thread)
st1=en
except:
print "Error, unable to start thread"
print '\t Number of threads active:',threading.activeCount()
for t in threads:
t.join()
print "Exiting Main Thread"
dict = collections.OrderedDict(sorted(dic.items()))
for key in dict:
print dict[key],'--->' 'Live'
t2=datetime.now()
total=t2-t1
print 'scanning complete in ' ,total