-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
124 lines (92 loc) · 2.58 KB
/
scanner.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
# multiproc_test.py
from time import sleep
import subprocess, platform, time, socket, sys, os
import multiprocessing
from datetime import datetime
class scanner:
# * * * * * * * * * * * * * * * *
def __init__(self):
osname = platform.system()
if osname == "Windows":
clear = lambda: os.system('cls')
elif osname == "Linux":
clear = lambda: os.system('clear')
else:
clear = lambda: os.system('')
clear()
# Determine the IP Addresses and the ports we are going to scan
# In the future, check what our DHCP settings are, and automatically set to scan entire current working subnet ;)
iplist = []
portlist = ['80']
# Set a range for IP's - let's scan the 192.168.1.1/24 subnet
iprange = range(90,92)
for i in iprange:
iplist.append("192.168.1."+str(i))
del i, iprange # We don't need these anymore
# Clear the screen
clear()
# Check what time the scan started
t1 = datetime.now()
# Set Jobs list
jobs = []
try:
for ip in iplist:
process = multiprocessing.Process(target=self.scan,args=(ip, portlist))
jobs.append(process)
# End ip in iplist
# Start the processes
for j in jobs:
j.start()
# Ensure all of the processes have finished
for j in jobs:
j.join()
except KeyboardInterrupt:
#print "You pressed Ctrl+C"
sys.exit()
# Checking the time again
t2 = datetime.now()
# Calculates the difference of time, to see how long it took to run the script
total = t2 - t1
# Printing the information to screen
print 'Scanning Completed in: ', total
# * * * * * * * * * * * * * * * *
# End Def init
# The scan function
def scan(self, ip, portlist):
try:
reportopen = ""
reportclose = ""
for port in portlist:
port = int(port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
result = sock.connect_ex((ip, port))
if result == 0:
reportopen += "\tOpen Port "+format(port)+"\n"
else:
reportclose += "\tClosed Port "+format(port)+"\n"
sock.close()
if reportopen != "":
print "~" * 60
print "IP: " + str(ip)
print reportopen
del reportopen
if reportclose != "":
print reportclose
del reportclose
sleep(1)
except KeyboardInterrupt:
#print "You pressed Ctrl+C"
sys.exit()
# * * * * * * * * * * * * * * * *
# End Def scan
if __name__ == '__main__':
multiprocessing.freeze_support()
#
# * * * * * * * * * * *
# # *
scanner = scanner() # * # Run the script!
# # *
# * * * * * * * * * * *
#