Spaces:
Running
Running
File size: 9,151 Bytes
765065a 7019a2d 765065a c7576e1 765065a c7576e1 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 765065a 7019a2d 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 277 278 279 280 |
#!/usr/bin/env python3
"""
MCP Tool: Auto-configure GreenLeaf Coffee network switches (HQ + dynamic stores)
Uses telnet to send initial configuration to all switches
"""
import telnetlib
import time
import requests
import re
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_actual_console_ports():
"""Extract real console ports from QEMU processes"""
import subprocess
result = subprocess.run(['ps', 'aux'], capture_output=True, text=True)
ports = {}
for line in result.stdout.split('\n'):
if 'qemu-system-x86_64' not in line:
continue
name = None
if 'SW-HQ-Core' in line:
name = 'SW-HQ-Core'
else:
m = re.search(r'SW-Store-(\d+)', line)
if m:
name = f"SW-Store-{m.group(1)}"
if not name:
continue
match = re.search(r'-serial telnet:127\.0\.0\.1:(\d+)', line)
if match:
ports[name] = int(match.group(1))
return ports
def configure_switch(host, port, switch_name, config_commands):
"""Send configuration to switch via telnet"""
print(f"\n{'='*60}")
print(f"Configuring {switch_name} on port {port}...")
print('='*60)
try:
tn = telnetlib.Telnet(host, port, timeout=10)
# Wake up console
tn.write(b"\r\n")
time.sleep(1)
# Read initial output
output = tn.read_very_eager().decode('utf-8', errors='ignore')
# Check if we need to press RETURN to start
if "Press RETURN to get started" in output or output.strip() == "":
print(" β Pressing RETURN to start...")
tn.write(b"\r\n")
time.sleep(2)
output = tn.read_very_eager().decode('utf-8', errors='ignore')
# Enter enable mode if at user prompt
if ">" in output:
print(" β Entering enable mode...")
tn.write(b"enable\r\n")
time.sleep(1)
tn.read_very_eager()
# Send each config command
for cmd in config_commands:
print(f" β {cmd}")
tn.write(cmd.encode('utf-8') + b"\r\n")
time.sleep(0.5)
response = tn.read_very_eager().decode('utf-8', errors='ignore')
# Check for errors
if "Invalid" in response or "Error" in response:
print(f" β οΈ Warning: {response[:100]}")
# Exit config mode
tn.write(b"end\r\n")
time.sleep(0.5)
# Save config
print(" β Saving configuration...")
tn.write(b"write memory\r\n")
time.sleep(2)
response = tn.read_very_eager().decode('utf-8', errors='ignore')
if "OK" in response or "[OK]" in response:
print(" β
Configuration saved successfully!")
tn.close()
return True
except Exception as e:
print(f" β Error: {e}")
return False
def main():
print("="*70)
print("πΏ GREENLEAF COFFEE - AUTOMATED NETWORK CONFIGURATION")
print("="*70)
print("\nUsing MCP automation to configure all switches...\n")
# Get actual console ports
print("π Discovering switch console ports...")
ports = get_actual_console_ports()
if not ports:
print("β No switches found! Make sure they're running.")
return
print(f"β
Found {len(ports)} switches:")
for name, port in ports.items():
print(f" β’ {name:20} β telnet://localhost:{port}")
def hq_config():
return [
"configure terminal",
"hostname SW-HQ-Core",
"!",
"! VLAN Configuration",
"vlan 10",
" name Sales",
"vlan 20",
" name Engineering",
"vlan 99",
" name Management",
"exit",
"!",
"! Management interface",
"interface vlan 99",
" ip address 10.99.0.1 255.255.255.0",
" no shutdown",
"exit",
"!",
"! WAN uplink",
"interface ethernet 15",
" description WAN to Internet/MPLS",
" switchport trunk encapsulation dot1q",
" switchport mode trunk",
" no shutdown",
"!",
"! Server port",
"interface ethernet 0",
" description Server-DB",
" switchport mode access",
" switchport access vlan 99",
" spanning-tree portfast",
" no shutdown",
"!",
"! Office PC port",
"interface ethernet 1",
" description Office-Manager",
" switchport mode access",
" switchport access vlan 99",
" spanning-tree portfast",
" no shutdown",
"!",
"! Enable SSH",
"ip domain-name greenleaf.local",
"crypto key generate rsa modulus 2048",
"!",
"username admin privilege 15 secret overgrowth123",
"line vty 0 4",
" login local",
" transport input ssh",
"!",
"! Banner",
"banner motd # GreenLeaf Coffee HQ - Authorized Access Only #",
]
def store_switch_config(name: str, idx: int) -> list:
mgmt_octet = 10 + idx if idx > 0 else 10
return [
"configure terminal",
f"hostname {name}",
"!",
"! VLAN Configuration",
"vlan 10",
" name Sales",
"vlan 99",
" name Management",
"exit",
"!",
"! Management interface",
"interface vlan 99",
f" ip address 10.99.0.{mgmt_octet} 255.255.255.0",
" no shutdown",
"exit",
"!",
"! Trunk to WAN",
"interface ethernet 15",
" description WAN to Internet/MPLS",
" switchport trunk encapsulation dot1q",
" switchport mode trunk",
" no shutdown",
"!",
"! POS Terminal",
"interface ethernet 0",
f" description POS-{name}",
" switchport mode access",
" switchport access vlan 10",
" spanning-tree portfast",
" no shutdown",
"!",
"! WiFi Access Point",
"interface ethernet 1",
f" description WiFi-{name}",
" switchport mode access",
" switchport access vlan 10",
" spanning-tree portfast",
" no shutdown",
"!",
"! Enable SSH",
"ip domain-name greenleaf.local",
"crypto key generate rsa modulus 2048",
"username admin privilege 15 secret overgrowth123",
"line vty 0 4",
" login local",
" transport input ssh",
"!",
f"banner motd # GreenLeaf Coffee {name} - Authorized Access Only #",
]
# Configure each switch
results = {}
for switch_name, port in ports.items():
if switch_name == 'SW-HQ-Core':
commands = hq_config()
elif switch_name.startswith('SW-Store-'):
idx_match = re.search(r'SW-Store-(\d+)', switch_name)
idx = int(idx_match.group(1)) if idx_match else 0
commands = store_switch_config(switch_name, idx)
else:
continue
success = configure_switch('localhost', port, switch_name, commands)
results[switch_name] = success
# Summary
print("\n" + "="*70)
print("CONFIGURATION SUMMARY")
print("="*70)
for switch, success in results.items():
status = "β
SUCCESS" if success else "β FAILED"
print(f" {switch:20} {status}")
if results:
if all(results.values()):
print("\nπ ALL SWITCHES CONFIGURED!")
else:
print("\nβ οΈ Some switches failed to configure")
print(" Check console output for errors")
else:
print("\nβ οΈ No switches were configured")
if results:
print("\nπ Network Details:")
print(" β’ HQ Management IP: 10.99.0.1")
for name in sorted(results.keys()):
if name.startswith("SW-Store-"):
m = re.search(r"SW-Store-(\d+)", name)
idx = int(m.group(1)) if m else 0
mgmt_ip = f"10.99.0.{10 + idx if idx > 0 else 10}"
print(f" β’ {name:18} Mgmt IP: {mgmt_ip}")
print("\nπ SSH Credentials:")
print(" β’ Username: admin")
print(" β’ Password: overgrowth123")
print("\n⨠Next: SSH into switches for remote management!")
print(" ssh [email protected]")
print("="*70)
if __name__ == "__main__":
main()
|