-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nmap_scanner.py
35 lines (26 loc) · 1.38 KB
/
Nmap_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
import argparse
import nmap
def argument_parser():
"""Allow user to specify target host and port."""
parser = argparse.ArgumentParser(description="TCP port scanner. Accepts a hostname/IP address and list of ports to "
"scan. Attempts to identify the service running on a port.")
parser.add_argument("-o", "--host", nargs="?", help="Host IP address")
parser.add_argument("-p", "--ports", nargs="?", help="Comma-separated port list, such as '25,80,8080'")
var_args = vars(parser.parse_args()) # Convert argument namespace to dictionary
return var_args
def nmap_scan(host_id, port_num):
"""Use nmap utility to check host ports for status."""
nm_scan = nmap.PortScanner()
nm_scan.scan(host_id, port_num)
state = nm_scan[host_id]['tcp'][int(port_num)]['state'] # Indicate the type of scan and port number
result = ("[*] {host} tcp/{port} {state}".format(host=host_id, port=port_num, state=state))
return result
if __name__ == "__main__":
try:
user_args = argument_parser()
host = user_args["host"]
ports = user_args["ports"].split(",") # Make a list from port numbers
for port in ports:
print(nmap_scan(host, port))
except AttributeError:
print("Error. Please provide the command-line arguments before running.")