Skip to content

Commit 88dda68

Browse files
authored
Create nmap_port_scanner_ip_obj.py
1 parent 9da3811 commit 88dda68

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

nmap_port_scanner_ip_obj.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#Use these commands in Kali to install required software:
2+
# sudo apt install python3-pip
3+
# pip install python-nmap
4+
5+
# Import nmap so we can use it for the scan
6+
import nmap
7+
# We import the ipaddress module. We want to use the ipaddress.ip_address(address)
8+
# method to see if we can instantiate a valid ip address to test.
9+
import ipaddress
10+
# We need to create regular expressions to ensure that the input is correctly formatted.
11+
import re
12+
13+
# Regular Expression Pattern to extract the number of ports you want to scan.
14+
# You have to specify <lowest_port_number>-<highest_port_number> (ex 10-100)
15+
port_range_pattern = re.compile("([0-9]+)-([0-9]+)")
16+
# Initialising the port numbers, will be using the variables later on.
17+
port_min = 0
18+
port_max = 65535
19+
20+
# This port scanner uses the Python nmap module.
21+
# You'll need to install the following to get it work on Linux:
22+
# Step 1: sudo apt install python3-pip
23+
# Step 2: pip install python-nmap
24+
25+
26+
# Basic user interface header
27+
print(r"""______ _ _ ______ _ _
28+
| _ \ (_) | | | ___ \ | | | |
29+
| | | |__ ___ ___ __| | | |_/ / ___ _ __ ___ | |__ __ _| |
30+
| | | / _` \ \ / / |/ _` | | ___ \/ _ \| '_ ` _ \| '_ \ / _` | |
31+
| |/ / (_| |\ V /| | (_| | | |_/ / (_) | | | | | | |_) | (_| | |
32+
|___/ \__,_| \_/ |_|\__,_| \____/ \___/|_| |_| |_|_.__/ \__,_|_|""")
33+
print("\n****************************************************************")
34+
print("\n* Copyright of David Bombal, 2021 *")
35+
print("\n* https://www.davidbombal.com *")
36+
print("\n* https://www.youtube.com/davidbombal *")
37+
print("\n****************************************************************")
38+
39+
# Ask user to input the ip address they want to scan.
40+
while True:
41+
ip_add_entered = input("\nPlease enter the ip address that you want to scan: ")
42+
# If we enter an invalid ip address the try except block will go to the except block and say you entered an invalid ip address.
43+
try:
44+
ip_address_obj = ipaddress.ip_address(ip_add_entered)
45+
# The following line will only execute if the ip is valid.
46+
print("You entered a valid ip address.")
47+
break
48+
except:
49+
print("You entered an invalid ip address")
50+
51+
52+
while True:
53+
# You can scan 0-65535 ports. This scanner is basic and doesn't use multithreading so scanning all the ports is not advised.
54+
print("Please enter the range of ports you want to scan in format: <int>-<int> (ex would be 60-120)")
55+
port_range = input("Enter port range: ")
56+
# We pass the port numbers in by removing extra spaces that people sometimes enter. So if you enter 80 - 90 instead of 80-90 the program will still work.
57+
port_range_valid = port_range_pattern.search(port_range.replace(" ",""))
58+
if port_range_valid:
59+
# We're extracting the low end of the port scanner range the user want to scan.
60+
port_min = int(port_range_valid.group(1))
61+
# We're extracting the upper end of the port scanner range the user want to scan.
62+
port_max = int(port_range_valid.group(2))
63+
break
64+
65+
nm = nmap.PortScanner()
66+
# We're looping over all of the ports in the specified range.
67+
for port in range(port_min, port_max + 1):
68+
try:
69+
# The result is quite interesting to look at. You may want to inspect the dictionary it returns.
70+
# It contains what was sent to the command line in addition to the port status we're after.
71+
# For in nmap for port 80 and ip 10.0.0.2 you'd run: nmap -oX - -p 89 -sV 10.0.0.2
72+
result = nm.scan(ip_add_entered, str(port))
73+
# Uncomment following line and look at dictionary
74+
# print(result)
75+
# We extract the port status from the returned object
76+
port_status = (result['scan'][ip_add_entered]['tcp'][port]['state'])
77+
print(f"Port {port} is {port_status}")
78+
except:
79+
# We cannot scan some ports and this ensures the program doesn't crash when we try to scan them.
80+
print(f"Cannot scan port {port}.")

0 commit comments

Comments
 (0)