Post

Nmap Scanner

In this post, I’ll show you how to build a simple Nmap scanner using Python.

Description

With just a few lines of code, you can automate basic scanning tasks like checking open ports or identifying live hosts, all powered by the Nmap tool underneath.

This script is great for learning how to integrate external tools into Python, and for understanding how scanning works in a controlled, legal environment.

⚠️ Disclaimer:
This script is for educational purposes only.
I’m not responsible for any misuse. Please use it ethically and legally.

💡 Make sure you have Nmap package installed on your system to run the script.

The full Python code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import nmap

# initialize the port scanner
scanner = nmap.PortScanner()
target = input("Please Enter An IP Address :")
# scan target for ports in range 1-1024
scanner.scan(target,"1-1024",'-sV')

#output the host name
print("This Host Name Is:" + scanner[target].hostname())
#output the host status up/no
print("The Host Status Is:" + scanner[target].state())

#output all open ports 
keys = scanner[target]['tcp'].keys()
#output all opens ports with informations
for i in keys:
    print('----------------------------')
    print('the port' + str(i) + " : ")
    #this line for information of ports 
    res = scanner[target]['tcp'][i]
    for re in res:
        print(re + " : " + res[re])

This script is inspired by a YouTube video I watched recently. Check out it from this link

This post is licensed under CC BY 4.0 by the author.