File size: 6,713 Bytes
55c0c24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Deploy initial configuration to GNS3 lab devices to set up management IPs.

This is a bootstrap script that configures management IPs on your GNS3 devices
so they can be managed via SSH/NAPALM in the main deployment workflow.
"""

import sys
import time
from pathlib import Path

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from agent.device_driver import DeviceType, DeviceCredentials
from agent.deployment_engine import DeploymentEngine, DeploymentTask

# Initial bootstrap configs - these set up management access
BOOTSTRAP_CONFIGS = {
    'SW-HQ-Core': """
enable
configure terminal
hostname SW-HQ-Core
!
ip domain-name lab.grahampaasch.com
!
interface Vlan1
 ip address 192.168.122.10 255.255.255.0
 no shutdown
!
ip route 0.0.0.0 0.0.0.0 192.168.122.1
!
username admin privilege 15 password cisco
enable secret cisco
!
line vty 0 4
 login local
 transport input ssh
!
crypto key generate rsa modulus 2048
!
end
write memory
""",
    'SW-West': """
enable
configure terminal
hostname SW-West
!
ip domain-name lab.grahampaasch.com
!
interface Vlan1
 ip address 192.168.122.20 255.255.255.0
 no shutdown
!
ip route 0.0.0.0 0.0.0.0 192.168.122.1
!
username admin privilege 15 password cisco
enable secret cisco
!
line vty 0 4
 login local
 transport input ssh
!
crypto key generate rsa modulus 2048
!
end
write memory
""",
    'SW-East': """
enable
configure terminal
hostname SW-East
!
ip domain-name lab.grahampaasch.com
!
interface Vlan1
 ip address 192.168.122.30 255.255.255.0
 no shutdown
!
ip route 0.0.0.0 0.0.0.0 192.168.100.1
!
username admin privilege 15 password cisco
enable secret cisco
!
line vty 0 4
 login local
 transport input ssh
!
crypto key generate rsa modulus 2048
!
end
write memory
"""
}

# Console port mapping
CONSOLE_PORTS = {
    'SW-HQ-Core': 5046,
    'SW-West': 5059,
    'SW-East': 5069,
}

def deploy_via_console(device_name, config, console_port):
    """
    Deploy config via console (telnet).
    
    For initial bootstrap when SSH isn't configured yet.
    """
    import telnetlib
    
    print(f"\n{'='*70}")
    print(f"Deploying to {device_name} via console port {console_port}")
    print(f"{'='*70}")
    
    try:
        tn = telnetlib.Telnet('localhost', console_port, timeout=10)
        
        # Wait for prompt
        print("  Waiting for device prompt...")
        time.sleep(2)
        
        # Send enters to wake up
        tn.write(b"\r\n\r\n")
        time.sleep(1)
        
        # Send config line by line
        print("  Sending configuration...")
        for line in config.strip().split('\n'):
            if line.strip():
                print(f"    > {line}")
                tn.write(line.encode('ascii') + b"\r\n")
                time.sleep(0.3)
        
        # Final newline
        tn.write(b"\r\n")
        time.sleep(2)
        
        # Get output
        output = tn.read_very_eager().decode('ascii', errors='ignore')
        
        tn.close()
        
        print(f"\n  ✅ Configuration deployed to {device_name}")
        print(f"  Management IP should be configured")
        
        return True
        
    except Exception as e:
        print(f"\n  ❌ Error deploying to {device_name}: {e}")
        return False

def verify_ssh_access(hostname, username='admin', password='cisco'):
    """
    Verify we can access device via SSH.
    """
    print(f"\nVerifying SSH access to {hostname}...")
    
    from agent.device_driver import DeviceDriver
    
    driver = DeviceDriver(use_napalm=True)
    
    creds = DeviceCredentials(
        hostname=hostname,
        username=username,
        password=password,
        device_type=DeviceType.CISCO_IOS,
        timeout=10
    )
    
    conn = driver.connect(creds)
    
    if conn.status.value == 'connected':
        print(f"  ✅ SSH access working!")
        driver.disconnect(hostname)
        return True
    else:
        print(f"  ⚠️  SSH not available yet: {conn.last_error}")
        return False

def main():
    """Main entry point"""
    
    print("\n" + "="*70)
    print("GNS3 Lab Bootstrap Configuration")
    print("="*70)
    print("\nThis script will:")
    print("  1. Deploy initial configs to devices via console (telnet)")
    print("  2. Configure management IPs (192.168.100.x)")
    print("  3. Enable SSH access for Stage 6 deployment")
    print("\nDevices:")
    for name, port in CONSOLE_PORTS.items():
        print(f"  - {name:15s} console port {port}")
    
    response = input("\nProceed with bootstrap? (yes/no): ")
    if response.lower() != 'yes':
        print("Aborted.")
        return 1
    
    # Deploy to each device
    success_count = 0
    for device_name in CONSOLE_PORTS.keys():
        config = BOOTSTRAP_CONFIGS.get(device_name)
        port = CONSOLE_PORTS[device_name]
        
        if deploy_via_console(device_name, config, port):
            success_count += 1
    
    print(f"\n{'='*70}")
    print(f"Bootstrap Summary: {success_count}/{len(CONSOLE_PORTS)} devices configured")
    print(f"{'='*70}")
    
    if success_count > 0:
        print("\n⏳ Waiting 30 seconds for devices to apply configs...")
        time.sleep(30)
        
        print("\nVerifying SSH access...")
        
        # Test SSH connectivity
        ssh_working = []
        for device_name in CONSOLE_PORTS.keys():
            ip = BOOTSTRAP_CONFIGS[device_name].split('ip address ')[1].split()[0]
            if verify_ssh_access(ip):
                ssh_working.append(device_name)
        
        print(f"\n{'='*70}")
        print("Next Steps:")
        print(f"{'='*70}")
        
        if ssh_working:
            print(f"\n✅ SSH working on {len(ssh_working)} devices!")
            print("\nYou can now use the main deployment script:")
            print("  python examples/deploy_to_gns3_lab.py --device SW-HQ-Core --dry-run")
            print("\nUpdate examples/deploy_to_gns3_lab.py with these IPs:")
            print("\nGNS3_DEVICES = [")
            configs = {
                'SW-HQ-Core': '192.168.122.10',
                'SW-West': '192.168.122.20',
                'SW-East': '192.168.122.30',
            }
            for name, ip in configs.items():
                print(f"    {{'name': '{name}', 'hostname': '{ip}', ...}},")
            print("]")
        else:
            print("\n⚠️  SSH not yet available.")
            print("Wait a bit longer and verify manually:")
            print("  ssh admin@192.168.100.10  (password: cisco)")
    
    return 0

if __name__ == '__main__':
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        print("\n\nAborted by user")
        sys.exit(1)