Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """Connect all nodes in the overgrowth CCNA topology""" | |
| import requests | |
| import json | |
| GNS3_API = "http://localhost:3080/v2" | |
| PROJECT_ID = "1f712057-b33d-433f-90bb-2b9b3804a95e" | |
| def get_nodes(): | |
| resp = requests.get(f"{GNS3_API}/projects/{PROJECT_ID}/nodes") | |
| nodes = resp.json() | |
| node_dict = {n['name']: n for n in nodes} | |
| return node_dict | |
| def create_link(node1_id, adapter1, port1, node2_id, adapter2, port2, desc=""): | |
| data = { | |
| "nodes": [ | |
| {"node_id": node1_id, "adapter_number": adapter1, "port_number": port1}, | |
| {"node_id": node2_id, "adapter_number": adapter2, "port_number": port2} | |
| ] | |
| } | |
| resp = requests.post(f"{GNS3_API}/projects/{PROJECT_ID}/links", json=data) | |
| if resp.status_code in [200, 201]: | |
| print(f" β {desc}") | |
| return True | |
| else: | |
| print(f" β {desc} - Error: {resp.status_code}") | |
| return False | |
| nodes = get_nodes() | |
| print("Connecting CCNA Topology Links...\n") | |
| links = 0 | |
| # PC2 -> SW1 (all switch ports use adapter 0) | |
| if 'PC2-Sales' in nodes and 'SW1-Access' in nodes: | |
| if create_link(nodes['PC2-Sales']['node_id'], 0, 0, | |
| nodes['SW1-Access']['node_id'], 0, 1, | |
| "PC2-Sales <-> SW1-Access (Port 1)"): | |
| links += 1 | |
| # SW1 -> SW2 (Trunk) | |
| if 'SW1-Access' in nodes and 'SW2-Core' in nodes: | |
| if create_link(nodes['SW1-Access']['node_id'], 0, 7, | |
| nodes['SW2-Core']['node_id'], 0, 0, | |
| "SW1-Access <-> SW2-Core (Trunk Port 7-0)"): | |
| links += 1 | |
| # PC3 -> SW2 | |
| if 'PC3-Engineering' in nodes and 'SW2-Core' in nodes: | |
| if create_link(nodes['PC3-Engineering']['node_id'], 0, 0, | |
| nodes['SW2-Core']['node_id'], 0, 1, | |
| "PC3-Engineering <-> SW2-Core (Port 1)"): | |
| links += 1 | |
| # PC4 -> SW2 | |
| if 'PC4-Engineering' in nodes and 'SW2-Core' in nodes: | |
| if create_link(nodes['PC4-Engineering']['node_id'], 0, 0, | |
| nodes['SW2-Core']['node_id'], 0, 2, | |
| "PC4-Engineering <-> SW2-Core (Port 2)"): | |
| links += 1 | |
| # SW2 -> SW3 (Trunk) | |
| if 'SW2-Core' in nodes and 'SW3-Access' in nodes: | |
| if create_link(nodes['SW2-Core']['node_id'], 0, 7, | |
| nodes['SW3-Access']['node_id'], 0, 7, | |
| "SW2-Core <-> SW3-Access (Trunk Port 7-7)"): | |
| links += 1 | |
| # PC6 -> SW3 | |
| if 'PC6-Guest' in nodes and 'SW3-Access' in nodes: | |
| if create_link(nodes['PC6-Guest']['node_id'], 0, 0, | |
| nodes['SW3-Access']['node_id'], 0, 1, | |
| "PC6-Guest <-> SW3-Access (Port 1)"): | |
| links += 1 | |
| # Internet -> SW2 | |
| if 'Internet' in nodes and 'SW2-Core' in nodes: | |
| if create_link(nodes['Internet']['node_id'], 0, 0, | |
| nodes['SW2-Core']['node_id'], 0, 15, | |
| "Internet Cloud <-> SW2-Core (Port 15)"): | |
| links += 1 | |
| print(f"\nβ Created {links} new links!") | |
| print(f"Total links should now be: {links + 2} (2 existing + {links} new)") | |