Cisco Network Discovery & Mapping Scripts
Write Python scripts that discover and map network topology from live devices. Follow these standards:
Discovery Methods
Ping Sweep
- ICMP sweep across subnets to find live hosts
- Use
concurrent.futures or asyncio for parallel pings
- Record response time and TTL (TTL helps identify device type/hop count)
- Support CIDR input (e.g.,
10.0.1.0/24) and ranges
- Identify likely network devices vs endpoints by TTL values (64=Linux/endpoint, 128=Windows, 255=Cisco)
Traceroute
- Trace paths between known endpoints to discover intermediate hops
- Build hop-by-hop path maps
- Identify asymmetric routing (trace both directions)
- Detect multiple paths (ECMP) by running repeated traces
- Record per-hop latency for path quality analysis
ARP Table Collection
- Pull
show ip arp from routers/L3 switches
- Map IP addresses to MAC addresses
- Identify MAC vendor (OUI lookup) to determine device manufacturer
- Detect duplicate IPs or MAC flapping
- Cross-reference with CAM/MAC address tables for port mapping
CDP (Cisco Discovery Protocol)
- Pull
show cdp neighbors detail from all Cisco devices
- Extract: device ID, platform, IP, local/remote interface, software version, capabilities
- Build adjacency graph from CDP data
- Crawl the network: start at seed device, discover neighbors, SSH to each, repeat
- Identify device roles from capabilities field (R=Router, S=Switch, T=Trans Bridge, etc.)
LLDP (Link Layer Discovery Protocol)
- Pull
show lldp neighbors detail for multi-vendor environments
- Extract: system name, system description, management IP, port ID, capabilities
- Use alongside CDP for complete visibility (non-Cisco devices only speak LLDP)
- Parse TLVs for additional info (VLAN, power, link aggregation)
Routing Table Analysis
- Pull
show ip route from all L3 devices
- Map all known subnets and next-hops
- Build a subnet-level topology (which device owns which subnet)
- Identify routing domains (OSPF areas, BGP AS numbers, EIGRP AS)
- Detect default route paths and internet egress points
- Pull
show ip ospf neighbor, show ip bgp summary, show ip eigrp neighbors for protocol-level adjacency
Discovery Workflow
1. Start with seed device(s) or subnet
2. Ping sweep to find live hosts
3. SSH to seed device → collect CDP/LLDP neighbors
4. Crawl: SSH to each discovered neighbor → repeat CDP/LLDP collection
5. Collect ARP tables from all L3 devices found
6. Collect routing tables from all routers found
7. Run traceroutes between key endpoints
8. Correlate all data into unified topology model
9. Output: topology map, device inventory, subnet map
Data Model
# Build a graph-based topology model
# Nodes = devices (router, switch, firewall, endpoint)
# Edges = links (with interface names, speed, subnet)
class Device:
hostname: str
ip_addresses: list # management + interface IPs
platform: str # e.g., "Cisco ISR 4451"
device_type: str # router, switch, firewall, endpoint
software: str # IOS version
interfaces: list # name, ip, mac, status, speed, neighbor
discovered_via: str # cdp, lldp, arp, ping, routing
class Link:
source_device: str
source_interface: str
target_device: str
target_interface: str
subnet: str
speed: str
protocol: str # cdp, lldp, or inferred
Network Crawl Safety
- Maintain a visited set to avoid loops
- Set a max hop depth (default: 10) to prevent runaway crawls
- Allow include/exclude filters by IP range or hostname pattern
- Respect rate limits (don't hammer devices with rapid SSH connects)
- Add delay between device connections (configurable, default 1 second)
- Set SSH timeout and retry limits
- Support read-only mode (only show commands, never config changes)
Output Formats
Text Report
- Device inventory table (hostname, IP, platform, IOS version)
- Interface-to-interface adjacency list
- Subnet map (which subnets on which devices)
- Routing summary per device
Structured Data
- JSON topology export (nodes + edges)
- CSV device inventory
- YAML topology for use with cisco-gns3 skill to rebuild in lab
Visual Diagram
- Use
graphviz (dot format) for topology diagrams
- Use
networkx + matplotlib for programmatic graph layout
- Use
draw.io XML export for editable diagrams
- Label nodes with hostname and IP
- Label edges with interface names and subnet
- Color code by device type (routers=blue, switches=green, firewalls=red)
- Group devices by site/subnet/routing domain
Libraries to Use
netmiko for SSH command execution
pyats + genie for structured show command parsing (preferred)
textfsm / ntc-templates as parsing alternative
scapy for custom ping/traceroute if ICMP from script host
subprocess for system ping/traceroute commands
ipaddress for subnet math
netaddr for MAC OUI vendor lookup
networkx for graph building and analysis
graphviz for diagram generation
matplotlib for visualization
concurrent.futures for parallel device polling
rich for progress bars and formatted console output
macvendors or OUI database for MAC-to-vendor resolution
Example Usage Patterns
Quick subnet scan
/cisco-network-map ping-sweep 10.0.0.0/24
Full crawl from seed device
/cisco-network-map crawl 192.168.1.1
Map routing topology
/cisco-network-map routing-topology R1,R2,R3
Generate diagram from collected data
/cisco-network-map diagram topology.json
Security Requirements
- NEVER hardcode credentials
- Use environment variables or
.env files (gitignored)
- Read-only operations only (no config commands)
- Log all devices accessed and commands run
- Support
--dry-run to show what would be discovered without connecting
1---2name: cisco-network-map3description: Generate Python scripts that discover and map network topology using ping, traceroute, ARP, CDP, LLDP, and routing tables. Use when the user wants to discover devices, map network topology, visualize network diagrams, or build a network inventory from live discovery.4---56## Cisco Network Discovery & Mapping Scripts78Write Python scripts that discover and map network topology from live devices. Follow these standards:910### Discovery Methods1112#### Ping Sweep13- ICMP sweep across subnets to find live hosts14- Use `concurrent.futures` or `asyncio` for parallel pings15- Record response time and TTL (TTL helps identify device type/hop count)16- Support CIDR input (e.g., `10.0.1.0/24`) and ranges17- Identify likely network devices vs endpoints by TTL values (64=Linux/endpoint, 128=Windows, 255=Cisco)1819#### Traceroute20- Trace paths between known endpoints to discover intermediate hops21- Build hop-by-hop path maps22- Identify asymmetric routing (trace both directions)23- Detect multiple paths (ECMP) by running repeated traces24- Record per-hop latency for path quality analysis2526#### ARP Table Collection27- Pull `show ip arp` from routers/L3 switches28- Map IP addresses to MAC addresses29- Identify MAC vendor (OUI lookup) to determine device manufacturer30- Detect duplicate IPs or MAC flapping31- Cross-reference with CAM/MAC address tables for port mapping3233#### CDP (Cisco Discovery Protocol)34- Pull `show cdp neighbors detail` from all Cisco devices35- Extract: device ID, platform, IP, local/remote interface, software version, capabilities36- Build adjacency graph from CDP data37- Crawl the network: start at seed device, discover neighbors, SSH to each, repeat38- Identify device roles from capabilities field (R=Router, S=Switch, T=Trans Bridge, etc.)3940#### LLDP (Link Layer Discovery Protocol)41- Pull `show lldp neighbors detail` for multi-vendor environments42- Extract: system name, system description, management IP, port ID, capabilities43- Use alongside CDP for complete visibility (non-Cisco devices only speak LLDP)44- Parse TLVs for additional info (VLAN, power, link aggregation)4546#### Routing Table Analysis47- Pull `show ip route` from all L3 devices48- Map all known subnets and next-hops49- Build a subnet-level topology (which device owns which subnet)50- Identify routing domains (OSPF areas, BGP AS numbers, EIGRP AS)51- Detect default route paths and internet egress points52- Pull `show ip ospf neighbor`, `show ip bgp summary`, `show ip eigrp neighbors` for protocol-level adjacency5354### Discovery Workflow5556```571. Start with seed device(s) or subnet582. Ping sweep to find live hosts593. SSH to seed device → collect CDP/LLDP neighbors604. Crawl: SSH to each discovered neighbor → repeat CDP/LLDP collection615. Collect ARP tables from all L3 devices found626. Collect routing tables from all routers found637. Run traceroutes between key endpoints648. Correlate all data into unified topology model659. Output: topology map, device inventory, subnet map66```6768### Data Model6970```python71# Build a graph-based topology model72# Nodes = devices (router, switch, firewall, endpoint)73# Edges = links (with interface names, speed, subnet)7475class Device:76 hostname: str77 ip_addresses: list # management + interface IPs78 platform: str # e.g., "Cisco ISR 4451"79 device_type: str # router, switch, firewall, endpoint80 software: str # IOS version81 interfaces: list # name, ip, mac, status, speed, neighbor82 discovered_via: str # cdp, lldp, arp, ping, routing8384class Link:85 source_device: str86 source_interface: str87 target_device: str88 target_interface: str89 subnet: str90 speed: str91 protocol: str # cdp, lldp, or inferred92```9394### Network Crawl Safety95- Maintain a visited set to avoid loops96- Set a max hop depth (default: 10) to prevent runaway crawls97- Allow include/exclude filters by IP range or hostname pattern98- Respect rate limits (don't hammer devices with rapid SSH connects)99- Add delay between device connections (configurable, default 1 second)100- Set SSH timeout and retry limits101- Support read-only mode (only show commands, never config changes)102103### Output Formats104105#### Text Report106- Device inventory table (hostname, IP, platform, IOS version)107- Interface-to-interface adjacency list108- Subnet map (which subnets on which devices)109- Routing summary per device110111#### Structured Data112- JSON topology export (nodes + edges)113- CSV device inventory114- YAML topology for use with cisco-gns3 skill to rebuild in lab115116#### Visual Diagram117- Use `graphviz` (dot format) for topology diagrams118- Use `networkx` + `matplotlib` for programmatic graph layout119- Use `draw.io` XML export for editable diagrams120- Label nodes with hostname and IP121- Label edges with interface names and subnet122- Color code by device type (routers=blue, switches=green, firewalls=red)123- Group devices by site/subnet/routing domain124125### Libraries to Use126- `netmiko` for SSH command execution127- `pyats` + `genie` for structured show command parsing (preferred)128- `textfsm` / `ntc-templates` as parsing alternative129- `scapy` for custom ping/traceroute if ICMP from script host130- `subprocess` for system ping/traceroute commands131- `ipaddress` for subnet math132- `netaddr` for MAC OUI vendor lookup133- `networkx` for graph building and analysis134- `graphviz` for diagram generation135- `matplotlib` for visualization136- `concurrent.futures` for parallel device polling137- `rich` for progress bars and formatted console output138- `macvendors` or OUI database for MAC-to-vendor resolution139140### Example Usage Patterns141142#### Quick subnet scan143```144/cisco-network-map ping-sweep 10.0.0.0/24145```146147#### Full crawl from seed device148```149/cisco-network-map crawl 192.168.1.1150```151152#### Map routing topology153```154/cisco-network-map routing-topology R1,R2,R3155```156157#### Generate diagram from collected data158```159/cisco-network-map diagram topology.json160```161162### Security Requirements163- NEVER hardcode credentials164- Use environment variables or `.env` files (gitignored)165- Read-only operations only (no config commands)166- Log all devices accessed and commands run167- Support `--dry-run` to show what would be discovered without connecting