[Day #32 PyATS Series] Tracing End-to-End Path (Multi-Hop Traceroute Validation) Using pyATS for Cisco [Python for Network Engineer]

[Day #32 PyATS Series] Tracing End-to-End Path (Multi-Hop Traceroute Validation) Using pyATS for Cisco [Python for Network Engineer]

Introduction

End-to-end path validation is a crucial part of network troubleshooting and performance analysis. Traceroute is one of the most widely used diagnostic tools to trace the path packets take across multiple hops in a network. However, manually running traceroute on individual devices and comparing results can be tedious and prone to errors.

In this tutorial, part of the 101 Days of pyATS (Vendor-Agnostic) series, Trainer Sagar Dhawan walks you through automating multi-hop traceroute validation using pyATS. Designed for Python for Network Engineer learners, this guide will show you how to:

  • Automate traceroute execution from multiple Cisco devices
  • Validate multi-hop paths for correctness and consistency
  • Detect routing anomalies or suboptimal paths
  • Generate detailed reports for troubleshooting and documentation

By the end of this post, you’ll have a robust solution for continuous end-to-end path validation.


Topology Overview

Our example network includes:

  • R1R2R3Destination Host
  • Multiple paths for redundancy
  • Goal: Verify that traffic consistently follows the expected multi-hop path.

Topology & Communications

  • Protocol: IP routing (OSPF/BGP)
  • Tool: traceroute command execution via pyATS
  • Authentication: Defined in testbed.yml
  • Execution: CLI-based SSH connections

Steps:

  1. Connect to each source router/device.
  2. Execute traceroute to the target destination.
  3. Parse and record hop-by-hop details.
  4. Validate against the expected routing path.

Workflow Script

from genie.testbed import load
import json

def run_traceroute(device, destination):
    device.connect(log_stdout=False)
    output = device.execute(f'traceroute {destination}')
    device.disconnect()
    return output

if __name__ == "__main__":
    testbed = load('testbed.yml')
    devices = testbed.devices
    
    destination_ip = '192.168.200.10'
    report = {}

    for name, device in devices.items():
        print(f"Running traceroute from {name}...")
        report[name] = run_traceroute(device, destination_ip)

    with open('traceroute_report.json', 'w') as f:
        json.dump(report, f, indent=4)

    print(json.dumps(report, indent=4))

Explanation by Line

  • run_traceroute function: Connects to a device, runs traceroute, and retrieves raw output.
  • Main block: Iterates through all devices in the testbed, executes traceroute to the destination IP, and stores results.
  • Output: JSON file with hop-by-hop path data from each device.

testbed.yml Example

testbed:
  name: traceroute_test
  devices:
    R1:
      os: iosxe
      type: router
      connections:
        cli:
          protocol: ssh
          ip: 192.168.101.11
      credentials:
        default:
          username: admin
          password: cisco123

    R2:
      os: iosxe
      type: router
      connections:
        cli:
          protocol: ssh
          ip: 192.168.101.12
      credentials:
        default:
          username: admin
          password: cisco123

    R3:
      os: iosxe
      type: router
      connections:
        cli:
          protocol: ssh
          ip: 192.168.101.13
      credentials:
        default:
          username: admin
          password: cisco123

Post-validation CLI Screenshots (Expected Output)

R1:

R1# traceroute 192.168.200.10
1 192.168.101.12
2 192.168.101.13
3 192.168.200.10

Script Output:

{
  "R1": "1 192.168.101.12\n2 192.168.101.13\n3 192.168.200.10",
  "R2": "1 192.168.101.13\n2 192.168.200.10",
  "R3": "1 192.168.200.10"
}

FAQs

1. Can pyATS traceroute validation detect asymmetric paths?

Yes. Running traceroute from multiple devices can highlight differences in hop sequences, helping identify asymmetric routing scenarios.


2. How does the script handle unreachable hops or timeouts?

The script captures * or timeout indicators from traceroute output. You can extend it to log these events and flag them as anomalies.


3. Is the traceroute command safe to run in production environments?

Yes. Traceroute uses diagnostic packets and does not modify any device configuration, making it safe for production testing.


4. Can I visualize the traceroute paths from the JSON output?

Absolutely. The JSON report can be imported into visualization tools like Grafana, Kibana, or custom D3.js-based applications to display graphical path maps.


5. Does pyATS natively parse traceroute output for structured data?

By default, pyATS captures raw output. However, you can create a custom Genie parser for structured hop-by-hop analysis.


6. Can this approach validate MPLS or VPN-specific paths?

Yes. The script can be adapted to use MPLS-specific traceroute commands or VPN traceroute options supported by your devices.


7. How scalable is this solution across a large network?

pyATS can handle multiple concurrent device connections, allowing you to perform traceroute validation from dozens or hundreds of devices efficiently.


8. Can I integrate this traceroute validation into a CI/CD pipeline?

Yes. The script can be part of automated network health checks, enabling continuous validation in DevNet-style CI/CD workflows.


YouTube Link

Watch the Complete Python for Network Engineer: Tracing End-to-End Path (Multi-Hop Traceroute Validation) Using pyATS for Cisco [Python for Network Engineer] Lab Demo & Explanation on our channel:

Master Python Network Automation, Ansible, REST API & Cisco DevNet
Master Python Network Automation, Ansible, REST API & Cisco DevNet
Master Python Network Automation, Ansible, REST API & Cisco DevNet
Why Robot Framework for Network Automation?

Join Our Training

Automating multi-hop traceroute validation helps detect routing issues proactively. Trainer Sagar Dhawan offers a 3-month instructor-led program covering Python, Ansible, APIs, and Cisco DevNet for Network Engineers. Master tools like pyATS and gain hands-on experience building real-world network automation solutions.

Join Our Training to elevate your career with advanced Python for Network Engineer skills.

Enroll Now & Future‑Proof Your Career
Emailinfo@networkjourney.com
WhatsApp / Call: +91 97395 21088