ORIGINAL PROJECT INTERFACE · GrandStay · original Python source; no devices connected · Fictional demo data · Back to portfolio

routing_automation.py

Original automation module from the submitted project. Lab credentials and the device inventory are not published.

# routing_automation.py
# Inter-VLAN Static Routing Engine
# Enables IP routing and routed SVIs for every VLAN on CORE-SW, then installs a
# single static default route from CORE-SW to EDGE-RTR and a matching static
# return route from EDGE-RTR back to the internal network. No dynamic routing
# protocol (e.g. OSPF) is used - this matches the scope defined in the project
# proposal, where routing exists only to make ACL enforcement meaningful.

from netmiko import ConnectHandler
from devices import INVENTORY, SVI_CONFIGS, STATIC_ROUTES


def provision_routing():
    print("[*] Initiating Inter-VLAN Routing Engine (Static Routing)...")

    # 1. Configure CORE-SW: enable routing + one SVI per VLAN + default route
    for device in INVENTORY["switches"]:
        if device["role"] == "core":
            try:
                net_connect = ConnectHandler(**{
                    k: v for k, v in device.items() if k not in ("role", "name", "block")
                })

                svi_cmds = ["ip routing"]
                for svi in SVI_CONFIGS:
                    svi_cmds.extend([
                        f"interface Vlan{svi['vlan']}",
                        f"ip address {svi['ip']} {svi['mask']}",
                        "no shutdown"
                    ])
                net_connect.send_config_set(svi_cmds)
                print(f"[SUCCESS] {device['name']} (Core L3 Switch) SVIs configured for all VLANs.")

                default_route = STATIC_ROUTES["core_default_route"]
                route_cmd = [
                    f"ip route {default_route['destination']} {default_route['mask']} "
                    f"{default_route['next_hop']}"
                ]
                net_connect.send_config_set(route_cmd)
                print(f"[SUCCESS] {device['name']} default route installed via "
                      f"{default_route['next_hop']} (EDGE-RTR) for internet-bound traffic.")

                net_connect.disconnect()
            except Exception as e:
                print(f"[ERROR] Failed routing configuration on device {device['name']} "
                      f"({device['host']}): {str(e)}")

    # 2. Configure EDGE-RTR: static return route back to the internal network
    for device in INVENTORY["routers"]:
        if device["role"] == "edge":
            try:
                net_connect = ConnectHandler(**{
                    k: v for k, v in device.items() if k not in ("role", "name", "block")
                })

                return_route = STATIC_ROUTES["edge_return_route"]
                route_cmd = [
                    f"ip route {return_route['destination']} {return_route['mask']} "
                    f"{return_route['next_hop']}"
                ]
                net_connect.send_config_set(route_cmd)
                print(f"[SUCCESS] {device['name']} static return route installed via "
                      f"{return_route['next_hop']} (CORE-SW) for internal VLAN reachability.")

                net_connect.disconnect()
            except Exception as e:
                print(f"[ERROR] Failed routing configuration on device {device['name']} "
                      f"({device['host']}): {str(e)}")


if __name__ == "__main__":
    provision_routing()