Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9c171ac

Browse files
authored
Create nmap_port_scanner.py
1 parent88dda68 commit9c171ac

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎nmap_port_scanner.py

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp