This project provides a simple yet powerful Python script for network host discovery and port scanning, leveraging the python-nmap library. It offers a menu-driven interface to perform various scanning operations on specified network ranges or individual IP addresses.
This script integrates core Nmap functionalities through python-nmap, offering the following capabilities:
- CIDR Network Device Discovery:
- Scans a given network range (e.g., 192.168.1.0/24) using Nmap's efficient ping scan (-sn) to identify active hosts.
- Quickly provides a list of live IP addresses in the specified CIDR block.
- Custom IP Range Host Discovery:
- Discovers live hosts within a user-defined IP range (e.g., 192.168.1.1-254).
- Also uses Nmap's ping scan (-sn) for fast host detection.
- Port and Service Scanning:
- Performs a detailed scan on a target IP address for a specified range of TCP ports.
- Utilizes SYN stealth scan (-sS) for efficiency and service version detection (-sV) to identify applications and their versions running on open ports.
Before running this script, ensure you have the following installed:
- Nmap: The network scanning engine itself.
-
Linux (Debian/Ubuntu):
sudo apt-get update
sudo apt-get install nmap -
Windows/macOS: Download the installer from the official Nmap website. Ensure Nmap is added to your system's PATH.
-
- Python 3: This script is written in Python 3.
- You can download it from the official Python website.
Follow these steps to set up the project and install necessary Python libraries:
-
Clone the Repository (if applicable) or Save the Script:
Save the provided Python code as network_scanner.py (or any other .py name you prefer) in a new directory. -
Create a Python Virtual Environment (Recommended):
A virtual environment ensures that the project's dependencies don't conflict with other Python projects on your system.
# Navigate to your project directory
cd your_project_directory# Create a virtual environment named 'venv'
python3 -m venv venv -
Activate the Virtual Environment:
-
On Linux/macOS:
source venv/bin/activate -
On Windows (Command Prompt):
venv\Scripts\activate.bat -
On Windows (PowerShell):
.\venv\Scripts\Activate.ps1
-
(You will see (venv) in your terminal prompt, indicating the virtual environment is active.)
- Install the python-nmap Library:
With your virtual environment active, install the required library:
pip install python-nmap
Once the prerequisites are met and the virtual environment is set up, you can run the script:
- Activate your virtual environment (if not already active, see step 3 in Installation).
- Run the script:
python network_scanner.py
The script will present a menu:
--- Python Network Scanner ---
1. Discover devices in a network (CIDR Ping Scan)
2. Discover devices in a custom IP range (e.g., 192.168.1.1-254)
3. Scan ports and detect services on a specific IP address
4. Exit
Enter your choice (1, 2, 3, or 4):
Follow the prompts to enter target IP ranges or specific IP addresses and port ranges.
Important Note on Permissions:
For certain Nmap scans (like SYN scans -sS used in port scanning) that require raw socket access, you might need to run the script with elevated privileges (e.g., sudo on Linux):
sudo python network_scanner.py
Always exercise caution when running scripts with sudo.
Always ensure you have explicit permission to scan any network or device you do not own or manage. Unauthorized network scanning can be illegal and unethical, and may lead to legal consequences or network disruption. This tool is intended for educational purposes, personal network administration, or penetration testing with proper authorization.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! If you have suggestions, bug reports, or want to add features, please open an issue or submit a pull request.