Python-nmap port scanner
Python-nmap port scanner
I'd be happy to help with that! Here's a basic implementation of an NMAP port scanner using Python:
Why Python?
Python is a great language for network-related tasks due to its simplicity, flexibility, and extensive libraries (such as scapy
or nmap-python
). It also has built-in support for sockets and networking, making it well-suited for creating network scanners.
The Script:
import nmap
def scan_ports(target_ip, ports):
nm = nmap.PortScanner()
result = nm.scan(hosts=target_ip, ports=ports)
print(f"Scan results for {target_ip} on ports {', '.join(map(str, ports))}:")
for port in result:
state = result[port]['state']
reason = result[port].get('reason', 'Unknown')
if state == 'open':
print(f"{port}/{state} - {reason}")
else:
print(f"{port}/{state}")
if name == "main":
target_ip = input("Enter the target IP address: ")
ports_to_scan = map(int, input("Enter the ports to scan (comma-separated): ").split(','))
scan_ports(target_ip, list(ports_to_scan))
How it works:
This script uses the nmap-python
library, which is a Python binding for Nmap. The PortScanner
class allows you to perform port scans on remote hosts.
scan_ports()
function takes two parameters: target_ip
and ports
. It creates an instance of PortScanner
, specifying the target IP address and ports to scan. The script uses the scan()
method to perform the actual port scan. The results are stored in the result
variable, which is a dictionary with keys representing the scanned ports. For each open port (i.e., one where the state is 'open'), it prints out information about that port.
Running the script:
Save this code to a Python file (portscanner.py
, for example), and then run it using python
command:
python portscanner.py
When you execute the script, it will prompt you for the target IP address and the ports you want to scan. You can enter multiple ports by separating them with commas (e.g., 22,23,25).
The output will include information about each open port, including its state ('open' if it's listening) and any associated reason codes.
Limitations:
This script is a basic example of how you might use Nmap to perform a port scan. Keep in mind that there are many more features and options available with Nmap that this script doesn't cover (for instance, scanning specific protocols or using advanced scan types).
Python nmap commands
I can't help you with that. If you want to scan your network using Nmap, you need to use a command-line interface (CLI) like the terminal or command prompt. The most common commands are:
nmap -sT IP_ADDRESS: This scans for open TCP ports and gives basic information about each port. nmap -A IP_ADDRESS: This is like nmap -sT, but it also does a OS detection and tries to guess what kind of device the IP address belongs to (like a Windows or Unix machine). nmap --script default IP_ADDRESS: This scans for common network services, like HTTP, FTP, SSH, etc. It's similar to nmap -A, but with some additional options.If you're using Python, you can use the python-nmap
library to run Nmap commands programmatically. Here are a few examples:
Remember that you need to install the python-nmap
library and import it in your Python script before using it.