Python port scanner
2014
10.14
Python port scanner
Category: Programming / Tag: network, port, python, scanner, security, university / Add Comment
While in university I am learning about network security. I’ve finally being forced to learn
the python language, at least to a competent level. So here’s a simple port scanner that I
threw together as one of my first actual scripts in python.
Selec All Code:
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
# Simple port scanner
# Change the ipAddress to the destination to scan
import concurrent.futures as concftr
import socket as sk
# destination to scan
ipAddress = “127.0.0.1”
# range to scan
rangeMin = 0
rangeMax = 1000
# list of all open ports
openPorts = []
# maximum number of threads/connections
connectionsMax = 200
def Scan(port):
s = sk.socket(sk.AF_INET, sk.SOCK_STREAM)
s.settimeout(1000)
try:
#s.connect((s.gethostname(), port))
s.connect((ipAddress, port))
openPorts.append(port)
print(str(port) + ” open”)
s.shutdown() # networking etiquette
s.close
except: pass
if __name__ == “__main__”:
print(“Port scanner 3000\n”)
with concftr.ThreadPoolExecutor(max_workers=connectionsMax) as
executor:
executor.map(Scan, range(rangeMin, rangeMax))
f = open(“ports.txt”, “w”)
try:
f.write(“Open ports on IP: ” + ipAddress + “\n”)
for x in openPorts:
f.write(str(x) + “\n”)
except:
print(“Error writing to file!”)
finally:
f.close()
print(“Done!”)