BGP Fault Finding With Python: A Step-by-Step Guide
From Idle to Established — and what to check at every stuck state.
Most BGP problems are diagnosable from one thing: which state the session is stuck in. Python lets you pull that state across the fleet and classify the cause in seconds.
The state machine
A healthy session climbs Idle → Connect → Active → OpenSent → OpenConfirm → Established. Where it stalls tells you what's wrong.
- Idle — no TCP attempt; check reachability, ACLs, and admin shutdown.
- Active / Connect — TCP failing; check port 179, firewalls, source interface.
- OpenSent — open message rejected; check ASN and BGP identifier mismatch.
- Established but no routes — check policy, max-prefix, and address families.
Pulling state
from netmiko import ConnectHandler
dev = ConnectHandler(device_type="cisco_ios", host="10.0.0.1",
username="netops", password="***")
out = dev.send_command("show ip bgp summary", use_textfsm=True)
for nbr in out:
if nbr["state_pfxrcd"].isdigit():
continue # Established
print(f"{nbr['bgp_neigh']} stuck in {nbr['state_pfxrcd']}")Turning output into findings
Wrap the classification in a parser that emits one finding per unhealthy neighbor with a stable correlation key like bgp/peer-down/10.0.0.2. Those findings feed the RCA engine and compliance checks.
Was this helpful?
Generate scripts for the task covered in this post
Describe what you need and NAPT writes production-ready automation in Netmiko, NAPALM, or Nornir.
Open Script Generator →Related posts
Compliance Without the Spreadsheet: Continuous Posture for Network Devices
The Compliance panel runs your hardening rule packs across the fleet, scores devices, and tracks drift so audits start with evidence, not promises.
Read →Inventory & IPAM: The Source of Truth Your Scripts Actually Trust
Upload a CSV, connect to NetBox, or generate an IP range — Inventory normalises every source into the same validated row shape for the platform.
Read →Runbooks: Living Documents That Run
Runbooks combine narrative, parameters, and executable scripts so on-call engineers stop choosing between reading the doc and fixing the problem.
Read →