-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_scanner.py
34 lines (30 loc) · 1.11 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
import nmap
import ipaddress
import re
port_range_pattern = re.compile("([0-9]+)-([0-9]+)")
port_min = 0
port_max = 65535
while True:
ip_add_entered = input("\nPlease enter the ip address that you want to scan: ")
try:
ip_address_obj = ipaddress.ip_address(ip_add_entered)
print("You entered a valid ip address.")
break
except:
print("You entered an invalid ip address")
while True:
print("Please enter the range of ports you want to scan in format: <int>-<int> (ex would be 60-120)")
port_range = input("Enter port range: ")
port_range_valid = port_range_pattern.search(port_range.replace(" ",""))
if port_range_valid:
port_min = int(port_range_valid.group(1))
port_max = int(port_range_valid.group(2))
break
nm = nmap.PortScanner()
for port in range(port_min, port_max + 1):
try:
result = nm.scan(ip_add_entered, str(port))
port_status = (result['scan'][ip_add_entered]['tcp'][port]['state'])
print(f"Port {port} is {port_status}")
except:
print(f"Cannot scan port {port}.")