-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_scanner.py
40 lines (31 loc) · 1.03 KB
/
port_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
import time
import socket
from collections import defaultdict
# a print_lock is what is used to prevent "double" modification of shared variables.
# this is used so while one thread is using a variable, others cannot access
# it. Once done, the thread releases the print_lock.
# to use it, you want to specify a print_lock per thing you wish to print_lock.
# ip = socket.gethostbyname(target)
def scanPort(port, ip):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, port))
out = (ip, port)
print(out)
s.close()
return out
except:
pass
def scanPorts(ip='localhost', no_threads=1, port_range=10000):
start = time.time()
out = defaultdict(list)
for ip in ip.split(','):
for port in range(1, port_range):
rst = scanPort(port, ip)
if rst:
out[rst[0]].append(str(rst[1]))
# out['duration'] = time.time() - start
return out
# def scanFromFile():
# for f in open('port_list.txt'):
# print(f)