This repository contains a script for discovering network devices using SNMP (Simple Network Management Protocol). The script recursively discovers devices, their neighbors, and the interfaces they use to connect to each other. The discovered information is stored in a structured JSON format.
- Python 3.6 or higher
pysnmp
librarylogging
library
-
Clone the repository:
https://github.com/KaSaNaa/SNMP-Discovery-Script.git cd SNMP-Discovery-Script
-
Create a virtual environment and activate it:
python3 -m venv venv source venv/bin/activate
-
Install the required dependencies:
pip install pysnmp
-
Update the SNMP configuration in
snmp_manager.py
:- Set the SNMP version, community string, and other SNMP parameters in the
SNMPManager
class.
- Set the SNMP version, community string, and other SNMP parameters in the
-
Run the script to perform recursive discovery on a single IP:
python main.py
The main.py
script performs recursive discovery on a single IP and saves the discovered devices to a JSON file.
import json
from utils.network_utils import NetworkUtils
from utils.snmp_manager import SNMPManager
from utils.graph_manager import GraphManager
# Example usage
if __name__ == "__main__":
snmp_manager = SNMPManager(version=2, community='public')
ip = '192.168.62.20'
result = snmp_manager.recursive_discovery(ip)
output_file_path = 'data/discovered_devices.json'
with open(output_file_path, 'w') as json_file:
json.dump(result, json_file, indent=4)
The SNMPManager
class provides methods for SNMP discovery, retrieving neighbors, and getting local ports.
snmp_discovery(target, base_oid='1.3.6.1.2.1.1', use_next_cmd=True)
: Performs SNMP discovery usingnextCmd
orgetCmd
.get_snmp_neighbors(ip)
: Retrieves SNMP neighbors using LLDP and CDP.get_local_ports(target)
: Retrieves local ports using the IF-MIB OID.recursive_discovery(ip, discovered_devices=None, discovered_ips=None)
: Recursively discovers devices and their neighbors.get_remote_interface(neighbor_ip, source_ip, local_port_oid)
: Retrieves the remote interface name for the connection betweensource_ip
andneighbor_ip
.hex_to_ip(hex_value)
: Converts a hex string to an IP address.
The output is a JSON file containing the discovered devices, their neighbors, and the interfaces they use to connect to each other. Here is a dummy example of what the output might look like:
{
"192.168.62.20": {
"hostname": "MainRouter",
"neighbors": {
"192.168.63.1": {
"hostname": "Neighbor1",
"local_interface": "Et0/0",
"remote_interface": "Gi0/1",
"details": {
"hostname": "Neighbor1",
"neighbors": {},
"ports": {
"1": "Et0/0",
"2": "Et0/1"
}
}
}
},
"ports": {
"1": "Et0/0",
"2": "Et0/1",
"3": "Et0/2",
"4": "Et0/3",
"5": "Vo0",
"6": "Nu0"
}
}
}
This project is licensed under the GNU GENERAL PUBLIC LICENSE.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. Open an issue or create a pull request to contribute.