forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport-scanner.py
50 lines (46 loc) · 1.59 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
41
42
43
44
45
46
47
48
49
50
#import libraries
import optparse
from socket import *
#function to connect to host and start scanning the specific port
def connScan(tgHost, tgPort):
try:
connSkt = socket(AF_INET, SOCK_STREAM)
connSkt.connect((tgHost, tgPort))
connSkt.send(b'HelloPython\r\n')
results = connSkt.recv(100)
print(f'[+] {tgPort}/tcp open')
print(f'[+] {results.decode()}')
connSkt.close()
except:
print(f'[-] {tgPort}/tcp closed')
#function to resolve the host and start port scan
def portScan(tgHost, tgPorts):
try:
tgIP = gethostbyname(tgHost)
except:
print(f"[-] Cannot resolve '{tgHost}': Unknown host")
return
try:
tgName = gethostbyaddr(tgIP)
print(f"\n[+] Scan Results for: {tgName[0]}")
except:
print(f"\n[+] Scan Results for: {tgIP}")
setdefaulttimeout(1)
for tgPort in tgPorts:
print(f"\nScanning port {tgPort}")
connScan(tgHost, tgPort)
#main function
def main():
parser = optparse.OptionParser()
parser.add_option('-H', "--Host", dest='tgHost', type='string', help='specify target host')
parser.add_option('-p', "--port", dest='tgPort', type='string', help='specify target port[s] separated by comma')
(options, args) = parser.parse_args()
tgHost = options.tgHost
tgPorts = str(options.tgPort).split(",")
if (tgHost == None) | (tgPorts[0] == None):
print("[-] You must specify a target host and port[s]")
exit(0)
tgPorts = [int(port) for port in tgPorts]
portScan(tgHost, tgPorts)
if __name__ == "__main__":
main()