This project is a Python script that scans IP addresses using Nmap and saves the results to a CSV file. It leverages the IP Fabric API to retrieve managed IP addresses and filters out private IP addresses.
- Python 3.9+
- IP Fabric account and API token
- Nmap installed on your system
- Python packages listed in the
requirements.txtfile:- ipfabric # matching you IP Fabric appliance version
- loguru
- pandas
- python3-nmap
- typer
- yaspin # Optional for spinner
-
Clone the repository:
git clone https://github.com/yourusername/ipf-nmap-ip.git cd ipf-nmap-ip -
Install the required Python packages:
pip install -r requirements.txt
-
Create a
.envfile in the project directory with the following variables, you can copy the.env.examplefile and modify it:IPF_URL=https://your-ipfabric-instance.com IPF_TOKEN=your_ipf_api_token IPF_VERIFY=True IPF_SNAPSHOT_ID=$last # IPs to exclude when collecting from IP Fabric IP_EXCLUDE_FILTER = "(^10\.)|(^172\.1[6-9])|(^172\.2[0-9])|(^172\.3[0-1])|(^192\.168)" # RFC1918 # NMAP ports to scan NMAP_PORTS="22-23,80,443"
-
Collect IP addresses from IP Fabric:
# Collect IP addresses not matching the IP_EXCLUDE_FILTER python ipf-nmap-ip.py collect # Collect IP addresses not matching the IP_EXCLUDE_FILTER but only keep public IP addresses (based on ipaddress.is_global) python ipf-nmap-ip.py collect --public # Specify the output file python ipf-nmap-ip.py collect --output collected_ips.csv --public
--output(optional): Name of the file to output the list of IPs to scan.--public(optional): Only keep public IP addresses from all IPs collected.
-
Scan the collected IP addresses:
# Scan IP addresses from a file to be selected interactively, from the `collected_ips` directory python ipf-nmap-ip.py scan # Scan IP addresses from a file, output will match the input file name python ipf-nmap-ip.py scan --input collected_ips.csv # Scan IP addresses from a file and specify the output file python ipf-nmap-ip.py scan --input collected_ips.csv --output scan_results.csv
--input(optional): Name of the file containing the IP addresses to scan.--output(optional): Name of the file to output the scan results.
If no
--inputis provided, the script will ask you to select a file from thecollected_ipsdirectory. -
Collect AND scan IP addresses in one step:
# Collect IP addresses and scan them python ipf-nmap-ip.py all # Collect IP addresses and scan them, output will match the input file name python ipf-nmap-ip.py all --output scan_results.csv --public
--output(optional): Name of the file to output the scan results.--public(optional): Only collect public IP addresses from IP Fabric.
- You can change the port(s) to be scanned by modifying the
NMAP_PORTSvariable in thesettings.pyfile or by setting theNMAP_PORTSenvironment variable. It has to be a string in the format22-23,80,443.
To use this script to perform the nmap scanning, without using IP Fabric, you need to provide a csv file with the following columns:
IP: The IP address to scan.Device: The device name (optional).Interface: The interface name (optional).
- The script uses the
logurulibrary for logging. Logs will be printed to the console and saved to a log file in thelogsdirectory.
This project is licensed under the MIT License. See the LICENSE file for details.