[Day #29 PyATS Series] Loopback IP Reachability Validation (Ping Tests) Using pyATS for Cisco [Python for Network Engineer]

[Day #29 PyATS Series] Loopback IP Reachability Validation (Ping Tests) Using pyATS for Cisco [Python for Network Engineer]

Introduction

In large-scale enterprise and service provider networks, Loopback interfaces play a crucial role in routing protocols, management access, and network stability. Ensuring continuous reachability of loopback IPs is a key part of proactive network monitoring. Manual ping testing can be tedious and prone to oversight, especially in multi-device environments.

In this tutorial, as part of the 101 Days of pyATS (Vendor-Agnostic) series, we will automate Loopback IP reachability validation using pyATS. Designed for Python for Network Engineer learners, this post teaches you how to:

  • Automate ICMP ping tests for loopback interfaces
  • Validate connectivity across Cisco devices
  • Generate detailed reports for troubleshooting
  • Scale the script to multi-vendor environments (Cisco, Arista, Palo Alto, Fortinet)

By the end, you’ll be able to integrate this automation into production pipelines, minimizing downtime and improving operational efficiency.


Topology Overview

  • Cisco Router1 with Loopback 0: 10.10.10.1/32
  • Cisco Router2 with Loopback 0: 10.10.20.1/32
  • Cisco Router3 with Loopback 0: 10.10.30.1/32

Goal: Ping test all loopback IPs from each device to ensure full reachability.


Topology & Communications

  • Protocol: ICMP echo requests via pyATS testcases
  • Authentication: Managed via testbed.yml
  • Execution: CLI connections (SSH or Console)

Workflow:

  1. Connect to each router.
  2. Retrieve loopback IPs.
  3. Perform ping tests to other routers’ loopbacks.
  4. Generate JSON and console reports.

Workflow Script

from genie.testbed import load
import json

def ping_loopbacks(device, loopbacks):
    device.connect(log_stdout=False)
    results = {}
    for ip in loopbacks:
        try:
            output = device.ping(ip)
            results[ip] = 'Reachable' if 'Success rate is 100 percent' in output else 'Unreachable'
        except Exception as e:
            results[ip] = f'Error: {str(e)}'
    device.disconnect()
    return results

if __name__ == "__main__":
    testbed = load('testbed.yml')
    devices = testbed.devices

    # Define loopback IPs for validation
    loopbacks = ['10.10.10.1', '10.10.20.1', '10.10.30.1']

    report = {}
    for name, device in devices.items():
        print(f"Validating loopback reachability from {name}...")
        report[name] = ping_loopbacks(device, loopbacks)

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

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

Explanation by Line

  • Imports: Load pyATS testbed, JSON for structured report.
  • ping_loopbacks function:
    • Establishes SSH connection.
    • Iterates through target loopback IPs.
    • Runs ping commands and parses results.
  • Main block:
    • Loads testbed, defines IPs.
    • Executes ping tests from each device.
    • Stores output in JSON format.

testbed.yml Example

testbed:
  name: loopback_ping_validation
  devices:
    Router1:
      os: iosxe
      type: router
      connections:
        cli:
          protocol: ssh
          ip: 192.168.100.1
      credentials:
        default:
          username: admin
          password: cisco123

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

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

Post-validation CLI Screenshots (Expected Output)

Cisco Router1:

Router1# ping 10.10.20.1
Success rate is 100 percent (5/5)

Script Output:

{
  "Router1": {
    "10.10.10.1": "Reachable",
    "10.10.20.1": "Reachable",
    "10.10.30.1": "Reachable"
  },
  "Router2": {
    "10.10.10.1": "Reachable",
    "10.10.20.1": "Reachable",
    "10.10.30.1": "Reachable"
  },
  "Router3": {
    "10.10.10.1": "Reachable",
    "10.10.20.1": "Reachable",
    "10.10.30.1": "Reachable"
  }
}

FAQs

1. Can this script validate loopback reachability in multi-vendor environments?

Yes. The script is designed to work with Cisco devices but can be extended to support Arista, Palo Alto, and Fortinet devices using pyATS Genie parsers or vendor-specific ping commands.

2. How can I dynamically fetch loopback IP addresses instead of hardcoding them?

You can modify the script to retrieve loopback IPs using commands like show ip interface brief and filter for loopback interfaces. Alternatively, you can load them from a CSV or database.

3. Does the script support IPv6 loopback ping tests?

Yes. By updating the list of IP addresses to include IPv6 addresses and ensuring devices support IPv6 ping commands, the script can validate IPv6 loopback reachability.

4. What happens if ICMP is blocked between devices?

If ICMP traffic is blocked, ping tests will fail, and the script will mark the IPs as unreachable. In such cases, consider using TCP or routing protocol-based reachability tests.

5. Can I schedule loopback reachability validation automatically?

Absolutely. You can use cron jobs or integrate this script into a CI/CD pipeline to run periodically and track results over time.

6. How scalable is this solution for large enterprise networks?

pyATS supports parallel device connections and distributed execution, making it scalable to validate loopback reachability across hundreds of devices efficiently.

7. Is it safe to run this script in production networks?

Yes. The script uses read-only ping commands, making it completely safe for production environments without impacting network operations.

8. Can the JSON report integrate with visualization tools?

Yes. The generated JSON report can be sent to visualization platforms like Grafana, ELK Stack, or custom dashboards to monitor loopback reachability health in real time.


YouTube Link

Watch the Complete Python for Network Engineer: Loopback IP Reachability Validation (Ping Tests) 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 loopback IP reachability checks is just one example of network validation with pyATS. Trainer Sagar Dhawan offers a comprehensive 3-month instructor-led program covering Python, Ansible, APIs, and Cisco DevNet for Network Engineers. Learn to build production-grade automation for your networks.

Join Our Training to master Python for Network Engineer and advance your career in network automation.

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