-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportscanner.py
66 lines (51 loc) · 1.92 KB
/
portscanner.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
# This script runs on Python 3
import socket, threading
import sys
import logging
def TCP_connect(ip, port_number, delay, output):
TCPsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
TCPsock.settimeout(delay)
try:
TCPsock.connect((ip, port_number))
output[port_number] = 'Listening'
except:
output[port_number] = ''
def scan_ports(host_ip, delay):
threads = [] # To run TCP_connect concurrently
output = {} # For printing purposes
# Spawning threads to scan ports
logging.debug('appending threads')
for i in range(500):
t = threading.Thread(target=TCP_connect, args=(host_ip, i, delay, output))
threads.append(t)
# Starting threads
logging.debug('finished appending')
for i in range(500):
try:
logging.debug('starting thread %s', threads[i])
threads[i].start()
except RuntimeError:
print("Run time error:", sys.exc_info()[0])
raise
# Locking the script until all threads complete
for i in range(500):
threads[i].join()
# Printing listening ports from small to large
for i in range(500):
if output[i] == 'Listening':
print(str(i) + ': ' + output[i])
logging.basicConfig(
level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',)
def run(target):
host_ip = target
delay = int(input("How many seconds the socket is going to wait until timeout: "))
print(target)
scan_ports(host_ip, delay)
def main():
host_ip = input("Enter host IP: ")
delay = int(input("How many seconds the socket is going to wait until timeout: "))
scan_ports(host_ip, delay)
if __name__ == "__main__":
main()