← All posts
How-To

BGP Fault Finding With Python: A Step-by-Step Guide

From Idle to Established — and what to check at every stuck state.

NAPT Team9 min read
BGP fault findingIDLECONNESTAB

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

python
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.

Pro tip: Always use a structured parser (TextFSM/Genie) instead of regex on raw output — vendor formatting shifts between versions and silently breaks brittle scrapers.
#bgp#how-to#python

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 →
New to NAPT? Start with the Setup Guide

Related posts