Port scanner using ‘python-nmap’
Last Updated :
21 Apr, 2020
In this article, we will learn how to program a port scanner using the ‘nmap
‘ module in Python. The program will take a range of port numbers as input and print the state (open or closed) of all the ports in that range.
Nmap: Nmap is a free and open-source network scanning tool. To run the program discussed in this article, you will need to have ‘nmap’ tool installed in your system. If it is not installed, visit Nmap download page.
Modules used
We will be using the ‘python-nmap
‘ module to achieve this task. Install the package (if not already installed) by the following command –
pip install python-nmap
Note: Doing ‘nmap’ scans on a target without proper permission and authority is illegal. Use localhost (127.0.0.1) as your target
Example:
import nmap
begin = 75
end = 80
target = '127.0.0.1'
scanner = nmap.PortScanner()
for i in range (begin,end + 1 ):
res = scanner.scan(target, str (i))
res = res[ 'scan' ][target][ 'tcp' ][i][ 'state' ]
print (f 'port {i} is {res}.' )
|
Output:
port 75 is closed.
port 76 is closed.
port 77 is closed.
port 78 is closed.
port 79 is closed.
port 80 is open.
Note: The output can vary depending on the present status of the ports.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...