-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest2.py
More file actions
40 lines (32 loc) · 1.01 KB
/
test2.py
File metadata and controls
40 lines (32 loc) · 1.01 KB
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
from netmiko import ConnectHandler
import json
# Load configuration from JSON file
with open("test2.json", "r") as file:
config = json.load(file)
# Extract router details
router = config['router']
# Establish connection to the router
connection = ConnectHandler(
host=router['host'],
username=router['username'],
password=router['password'],
device_type=router['device_type']
)
# Build configuration commands
commands = [
f"interface GigabitEthernet0/0",
f"ip address {router['management_ip']} 255.255.255.0",
"no shutdown"
]
for i, ip in enumerate(router['loopbacks'], start=1):
commands.append(f"interface Loopback{i}")
commands.append(f"ip address {ip} 255.255.255.255")
# Send commands to the router and display the output
output = connection.send_config_set(commands)
print("Configuration Output:\n")
print(output)
# Save configuration
save_output = connection.save_config()
print("\nConfiguration Saved.")
# Disconnect
connection.disconnect()