Context & Why
At 2:15 AM on a Saturday, our secondary core switch quietly dropped BGP peering with an upstream transit provider. The scheduled maintenance window had closed ten minutes earlier. The running configuration looked untouched, and physical interfaces showed up/up, yet 14 edge subnets were unreachable.
PagerDuty fired every four minutes. Four engineers sat in a bridge call, running show ip route, show bgp summary, and show vlan across eight chassis switches. We were manually eyeballing 2,800 lines of raw terminal output at 2:30 in the morning.
Sleep-deprived engineers miss dropped routes, subtle metric changes, and missing ARP entries under pressure. Staring at raw terminal text during a high-stakes change window is a recipe for missed outages. Network test automation fixes this by treating operational state as structured data rather than unformatted CLI text.
Cisco pyATS and its operational parsing engine, Genie, capture a programmatic snapshot of your network state before you touch a device. You make your changes, take a second snapshot, and diff the two trees. The framework flags missing prefixes, neighbor drops, or interface errors instantly.
That 2:00 AM scramble was the turning point for our team. We integrated pyATS snapshots into our standard change checklist across 35 production switches. Today, we get an exact, machine-readable verdict in under 45 seconds instead of spending 40 minutes hunting down a dropped route.
Installation
You can run pyATS inside any standard Python virtual environment on Linux or macOS. Stick with Python 3.10 or 3.11, and isolate your dependencies early to prevent system-level package conflicts.
# Create and activate an isolated virtual environment
python3 -m venv ~/pyats-env
source ~/pyats-env/bin/activate
# Upgrade packaging tools and install pyATS with full Genie libraries
pip install --upgrade pip setuptools wheel
pip install "pyats[full]"
Verify that both the core framework and operational parser libraries are ready:
pyats version check
genie version
When the output displays active versions for pyats, genie, and unicon, your environment is ready to connect to live gear.
Configuration
Device connections in pyATS revolve around a YAML testbed file. You define target management IPs, credentials, operating systems, and connection protocols in one centralized inventory.
Create a working directory and save your device inventory as testbed.yaml:
testbed:
name: production-core-testbed
credentials:
default:
username: netadmin
password: "EncryptedOrVaultPasswordHere"
devices:
core-sw01:
alias: core01
os: iosxe
type: switch
connections:
cli:
protocol: ssh
ip: 10.10.10.1
port: 22
dist-rtr01:
alias: dist01
os: iosxe
type: router
connections:
cli:
protocol: ssh
ip: 10.10.10.2
port: 22
Run a quick syntax and connection check before touching operational states:
pyats validate testbed testbed.yaml
Pre-Maintenance Snapshot via CLI
Genie includes native CLI subcommands to parse operational features straight into JSON. The parse command translates individual CLI outputs, while learn pulls together multiple commands to construct a complete feature model for routing, BGP, or interfaces.
# Capture state across all testbed devices prior to configuration changes
pyats parse "show ip interface brief" --testbed-file testbed.yaml --output pre-snap-intf
pyats learn "routing" --testbed-file testbed.yaml --output pre-snap-routes
pyats learn "bgp" --testbed-file testbed.yaml --output pre-snap-bgp
Automated Snapshot Script in Python
While CLI commands work well for quick operational tasks, the Python SDK gives you complete programmatic control for CI/CD pipelines and custom maintenance scripts:
#!/usr/bin/env python3
import json
from genie.testbed import load
# Load the testbed topology
testbed = load("testbed.yaml")
device = testbed.devices["core-sw01"]
# Open SSH connection
device.connect(log_stdout=False)
# Parse operational command output into native Python dictionaries
routing_state = device.learn("routing")
interface_state = device.parse("show ip interface brief")
# Save baseline snapshot to disk
with open("core-sw01-routing-pre.json", "w") as f:
json.dump(routing_state.info, f, indent=2)
print("[+] Pre-change operational state captured successfully.")
device.disconnect()
Verification & Monitoring
Once your network changes are committed, execute a post-change snapshot run and immediately compare the two states.
Capturing Post-Change State
# Capture identical features post-change
pyats learn "routing" --testbed-file testbed.yaml --output post-snap-routes
pyats learn "bgp" --testbed-file testbed.yaml --output post-snap-bgp
Calculating Operational Diffs
Standard text diff tools fail on network outputs because timestamps, interface packet counters, and memory metrics fluctuate continuously. The pyats diff command evaluates structured JSON trees instead. It ignores changing timestamps and zeroes in on actual operational shifts, such as missing prefixes, flapping neighbors, or interface admin status changes.
# Compare pre and post snapshots
pyats diff pre-snap-routes post-snap-routes --output diff-routes
pyats diff pre-snap-bgp post-snap-bgp --output diff-bgp
Take a look at the generated report in diff-routes/diff_routing.txt. A typical failure looks like this:
--- pre-snap-routes/routing_core-sw01.json
+++ post-snap-routes/routing_core-sw01.json
- "192.168.50.0/24": {
- "source_protocol": "bgp",
- "metric": 0,
- "route": "192.168.50.0/24"
- }
+ "192.168.50.0/24": null
That delta immediately confirms that subnet 192.168.50.0/24 dropped out of the routing table. You spot the failure in 15 seconds instead of waiting for a service desk escalation.
Programmatic Pre/Post Assertion in Python
You can embed state comparisons directly into automated deployment pipelines or rollback triggers. If any drift occurs, the script exits with an error code and halts the deployment:
#!/usr/bin/env python3
import json
import sys
from genie.utils.diff import Diff
from genie.testbed import load
testbed = load("testbed.yaml")
dev = testbed.devices["core-sw01"]
dev.connect(log_stdout=False)
# Capture post-change routing state
post_routing = dev.learn("routing").info
# Load pre-change baseline
with open("core-sw01-routing-pre.json") as f:
pre_routing = json.load(f)
# Compute programmatic diff
diff = Diff(pre_routing, post_routing)
diff.find_diff()
if diff.diffs:
print("[!] ERROR: Operational state drift detected post-maintenance!")
print(diff)
dev.disconnect()
sys.exit(1)
else:
print("[+] PASS: Device routing state matches pre-maintenance baseline.")
dev.disconnect()
sys.exit(0)
Adding this automated validation to your change checklist cuts verification time from hours to seconds. When changes succeed, you have structured proof of network health. When they fail, you pinpoint the exact missing route before anyone calls.
