-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathportscan.py
More file actions
63 lines (57 loc) · 1.78 KB
/
portscan.py
File metadata and controls
63 lines (57 loc) · 1.78 KB
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
import socket
import re
import sys
import errno
from threading import *
screen_lock = Semaphore(value=1)
def portscan(hostname, portnumber):
sn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sn.connect_ex((hostname,portnumber))
if result == 0:
screen_lock.acquire()
print (("\t%d \topen") %(portnumber))
sn.close()
screen_lock.release()
else:
screen_lock.acquire()
print (("\t%d \tclosed") % (portnumber))
screen_lock.release()
return
def checkiforfqdn(hostname, portnumber):
hn = hostname
portlist = portnumber.split(',')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ipchk = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
chk = ipchk.match(hn)
print ("\n\n--------------------------------------------------------------------------------")
print ("\n\t\t\t Scanning: " + hn.upper())
print ("--------------------------------------------------------------------------------")
if not chk:
try:
s = socket.gethostbyname(hn)
c = 0
for c in range(len(portlist)):
pl = int(portlist[c])
t = Thread(target=portscan, args=(hn, pl))
t.start()
c = c + 1
except socket.gaierror:
print ("\nCould not resolve the hostname " + hn.upper() + " please check the hostname")
else:
c = 0
for c in range(len(portlist)):
pl = int(portlist[c])
t = Thread(target=portscan, args=(hn, pl))
t.start()
c = c + 1
return
def main():
ac = len(sys.argv)
if ac == 3:
checkiforfqdn(sys.argv[1], sys.argv[2])
else:
print ("\n\n--------------------------------------------------------------------------------")
print ("\n\t Usage: portscan.py <hostname or IP> <comma separated list of TCP Ports>")
print ("\t for example: portscan.py 1.1.1.1 1,2,3,4")
return
main()