File size: 10,608 Bytes
765065a
 
 
 
 
 
 
 
 
c7576e1
765065a
c7576e1
 
 
765065a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
"""
Create a CCNA-ready topology using built-in GNS3 devices
Uses: Ethernet switches, VPCS for PCs, and Cloud for WAN simulation
"""

import requests
import json
import time
import os

GNS3_SERVER = os.getenv("GNS3_SERVER", "http://localhost:3080")
GNS3_API = f"{GNS3_SERVER}/v2"
PROJECT_NAME = os.getenv("GNS3_PROJECT_NAME", "overgrowth")

def get_project_id(name):
    resp = requests.get(f"{GNS3_API}/projects")
    projects = resp.json()
    project = next((p for p in projects if p['name'] == name), None)
    return project['project_id'] if project else None

def delete_all_nodes(project_id):
    resp = requests.get(f"{GNS3_API}/projects/{project_id}/nodes")
    nodes = resp.json()
    print(f"Cleaning up {len(nodes)} existing nodes...")
    for node in nodes:
        requests.delete(f"{GNS3_API}/projects/{project_id}/nodes/{node['node_id']}")
    time.sleep(1)

def create_vpcs(project_id, name, x, y):
    """Create a VPCS (Virtual PC)"""
    data = {
        "name": name,
        "node_type": "vpcs",
        "compute_id": "local",
        "x": x,
        "y": y
    }
    resp = requests.post(f"{GNS3_API}/projects/{project_id}/nodes", json=data)
    return resp.json() if resp.status_code in [200, 201] else None

def create_switch(project_id, name, x, y, ports=8):
    """Create an Ethernet switch"""
    data = {
        "name": name,
        "node_type": "ethernet_switch",
        "compute_id": "local",
        "x": x,
        "y": y,
        "properties": {
            "ports_mapping": [
                {"name": f"Ethernet{i}", "port_number": i, "type": "access", "vlan": 1}
                for i in range(ports)
            ]
        }
    }
    resp = requests.post(f"{GNS3_API}/projects/{project_id}/nodes", json=data)
    return resp.json() if resp.status_code in [200, 201] else None

def create_cloud(project_id, name, x, y):
    """Create a Cloud node (simulates WAN/Internet)"""
    data = {
        "name": name,
        "node_type": "cloud",
        "compute_id": "local",
        "x": x,
        "y": y
    }
    resp = requests.post(f"{GNS3_API}/projects/{project_id}/nodes", json=data)
    return resp.json() if resp.status_code in [200, 201] else None

def create_link(project_id, node1_id, adapter1, port1, node2_id, adapter2, port2):
    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)
    return resp.json() if resp.status_code in [200, 201] else None

def main():
    project_id = get_project_id(PROJECT_NAME)
    if not project_id:
        print(f"Project '{PROJECT_NAME}' not found!")
        return
    
    print(f"Building CCNA Topology in: {PROJECT_NAME}\n")
    
    delete_all_nodes(project_id)
    
    print("="*70)
    print("CCNA LAB TOPOLOGY - Network Fundamentals Practice")
    print("="*70)
    print("""
    Network Design:
    
    VLAN 10 (Sales)          VLAN 20 (Engineering)        VLAN 30 (Guest)
         PC1                      PC3                          PC5
          |                        |                            |
    ┌────┴────┐              ┌────┴────┐                  ┌────┴────┐
    │  SW1    │─────────────│  SW2    │──────────────────│  SW3    │
    │ Access  │   Trunk      │  Core   │     Trunk        │ Access  │
    └────┬────┘              └────┬────┘                  └────┬────┘
         |                        |                            |
        PC2                      PC4                          PC6
    VLAN 10                  VLAN 20                      VLAN 30
    
    Internet Cloud (optional WAN simulation)
    
    CCNA Skills Practiced:
    - VLAN configuration and trunking
    - Layer 2 switching concepts
    - Inter-VLAN routing (when routers added)
    - Basic connectivity testing
    - Network troubleshooting
    """)
    print("="*70 + "\n")
    
    nodes = {}
    
    # Create switches
    print("Creating Switches...")
    nodes['SW1'] = create_switch(project_id, "SW1-Access", -300, 0, ports=8)
    if nodes['SW1']: print(f"  ✓ SW1-Access (VLAN 10 - Sales)")
    time.sleep(0.5)
    
    nodes['SW2'] = create_switch(project_id, "SW2-Core", 0, 0, ports=16)
    if nodes['SW2']: print(f"  ✓ SW2-Core (Trunk/Core)")
    time.sleep(0.5)
    
    nodes['SW3'] = create_switch(project_id, "SW3-Access", 300, 0, ports=8)
    if nodes['SW3']: print(f"  ✓ SW3-Access (VLAN 20 - Engineering)")
    time.sleep(0.5)
    
    # Create PCs
    print("\nCreating Virtual PCs (VPCS)...")
    nodes['PC1'] = create_vpcs(project_id, "PC1-Sales", -400, -100)
    if nodes['PC1']: print(f"  ✓ PC1-Sales (VLAN 10)")
    time.sleep(0.3)
    
    nodes['PC2'] = create_vpcs(project_id, "PC2-Sales", -400, 100)
    if nodes['PC2']: print(f"  ✓ PC2-Sales (VLAN 10)")
    time.sleep(0.3)
    
    nodes['PC3'] = create_vpcs(project_id, "PC3-Engineering", -100, -100)
    if nodes['PC3']: print(f"  ✓ PC3-Engineering (VLAN 20)")
    time.sleep(0.3)
    
    nodes['PC4'] = create_vpcs(project_id, "PC4-Engineering", 100, -100)
    if nodes['PC4']: print(f"  ✓ PC4-Engineering (VLAN 20)")
    time.sleep(0.3)
    
    nodes['PC5'] = create_vpcs(project_id, "PC5-Guest", 400, -100)
    if nodes['PC5']: print(f"  ✓ PC5-Guest (VLAN 30)")
    time.sleep(0.3)
    
    nodes['PC6'] = create_vpcs(project_id, "PC6-Guest", 400, 100)
    if nodes['PC6']: print(f"  ✓ PC6-Guest (VLAN 30)")
    time.sleep(0.3)
    
    # Create Cloud for Internet/WAN simulation
    print("\nCreating WAN Simulation...")
    nodes['Internet'] = create_cloud(project_id, "Internet", 0, 150)
    if nodes['Internet']: print(f"  ✓ Internet Cloud (WAN)")
    time.sleep(0.5)
    
    # Create links
    print("\nCreating Network Links...")
    links = 0
    
    # PC1 -> SW1
    if nodes.get('PC1') and nodes.get('SW1'):
        if create_link(project_id, nodes['PC1']['node_id'], 0, 0, nodes['SW1']['node_id'], 0, 0):
            print(f"  ✓ PC1-Sales <-> SW1 (Port 0)")
            links += 1
    
    # PC2 -> SW1
    if nodes.get('PC2') and nodes.get('SW1'):
        if create_link(project_id, nodes['PC2']['node_id'], 0, 0, nodes['SW1']['node_id'], 1, 0):
            print(f"  ✓ PC2-Sales <-> SW1 (Port 1)")
            links += 1
    
    # SW1 -> SW2 (Trunk)
    if nodes.get('SW1') and nodes.get('SW2'):
        if create_link(project_id, nodes['SW1']['node_id'], 7, 0, nodes['SW2']['node_id'], 0, 0):
            print(f"  ✓ SW1 <-> SW2 (Trunk)")
            links += 1
    
    # PC3 -> SW2 (could be connected directly for mixed topology)
    if nodes.get('PC3') and nodes.get('SW2'):
        if create_link(project_id, nodes['PC3']['node_id'], 0, 0, nodes['SW2']['node_id'], 1, 0):
            print(f"  ✓ PC3-Engineering <-> SW2 (Port 1)")
            links += 1
    
    # PC4 -> SW2
    if nodes.get('PC4') and nodes.get('SW2'):
        if create_link(project_id, nodes['PC4']['node_id'], 0, 0, nodes['SW2']['node_id'], 2, 0):
            print(f"  ✓ PC4-Engineering <-> SW2 (Port 2)")
            links += 1
    
    # SW2 -> SW3 (Trunk)
    if nodes.get('SW2') and nodes.get('SW3'):
        if create_link(project_id, nodes['SW2']['node_id'], 7, 0, nodes['SW3']['node_id'], 7, 0):
            print(f"  ✓ SW2 <-> SW3 (Trunk)")
            links += 1
    
    # PC5 -> SW3
    if nodes.get('PC5') and nodes.get('SW3'):
        if create_link(project_id, nodes['PC5']['node_id'], 0, 0, nodes['SW3']['node_id'], 0, 0):
            print(f"  ✓ PC5-Guest <-> SW3 (Port 0)")
            links += 1
    
    # PC6 -> SW3
    if nodes.get('PC6') and nodes.get('SW3'):
        if create_link(project_id, nodes['PC6']['node_id'], 0, 0, nodes['SW3']['node_id'], 1, 0):
            print(f"  ✓ PC6-Guest <-> SW3 (Port 1)")
            links += 1
    
    # Internet -> SW2 (for future router uplink)
    if nodes.get('Internet') and nodes.get('SW2'):
        if create_link(project_id, nodes['Internet']['node_id'], 0, 0, nodes['SW2']['node_id'], 15, 0):
            print(f"  ✓ Internet Cloud <-> SW2 (Port 15)")
            links += 1
    
    # Summary
    print("\n" + "="*70)
    print("✓ CCNA LAB TOPOLOGY CREATED!")
    print("="*70)
    print(f"\nDevices: {len([n for n in nodes.values() if n])} nodes")
    print(f"Links:   {links} connections")
    
    print("\n📚 CCNA LAB EXERCISES:")
    print("\n  Basic Configuration:")
    print("    • Configure VLANs 10, 20, 30 on all switches")
    print("    • Set trunk ports between SW1-SW2 and SW2-SW3")
    print("    • Assign access ports to appropriate VLANs")
    print("    • Configure PC IP addresses in respective subnets")
    
    print("\n  IP Addressing Scheme:")
    print("    VLAN 10 (Sales):       10.1.10.0/24")
    print("      - PC1:               10.1.10.10")
    print("      - PC2:               10.1.10.11")
    print("    VLAN 20 (Engineering): 10.1.20.0/24")
    print("      - PC3:               10.1.20.10")
    print("      - PC4:               10.1.20.11")
    print("    VLAN 30 (Guest):       10.1.30.0/24")
    print("      - PC5:               10.1.30.10")
    print("      - PC6:               10.1.30.11")
    
    print("\n  VPCS Configuration Commands:")
    print("    On PC1: ip 10.1.10.10/24 10.1.10.1")
    print("    On PC2: ip 10.1.10.11/24 10.1.10.1")
    print("    On PC3: ip 10.1.20.10/24 10.1.20.1")
    print("    On PC4: ip 10.1.20.11/24 10.1.20.1")
    print("    On PC5: ip 10.1.30.10/24 10.1.30.1")
    print("    On PC6: ip 10.1.30.11/24 10.1.30.1")
    
    print("\n  Testing:")
    print("    • Ping between PCs in same VLAN (should work)")
    print("    • Ping between PCs in different VLANs (will fail without router)")
    print("    • Use 'show vlan' to verify VLAN configuration")
    print("    • Use 'show interfaces trunk' to verify trunk links")
    
    print("\n  Next Steps (When adding routers):")
    print("    • Add router for inter-VLAN routing")
    print("    • Configure router-on-a-stick (802.1Q)")
    print("    • Setup DHCP server on router")
    print("    • Configure NAT/PAT for Internet access")
    
    print("\n🚀 Ready to start! Open GNS3 GUI to begin configuration.")
    print("="*70)

if __name__ == "__main__":
    main()