forked from ine-content/devnet-github-branching
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect_routers.py
More file actions
32 lines (28 loc) · 973 Bytes
/
connect_routers.py
File metadata and controls
32 lines (28 loc) · 973 Bytes
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
import yaml
import paramiko
def load_router_details(yaml_file):
with open(yaml_file, 'r') as file:
return yaml.safe_load(file)
def connect_to_router(router):
print(f"Connecting to {router['hostname']} at {router['ip']}...")
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
hostname=router['ip'],
username=router['username'],
password=router['password']
)
print(f"Connected to {router['hostname']}")
# Example: Run a command
stdin, stdout, stderr = ssh.exec_command("show version")
print(stdout.read().decode())
ssh.close()
except Exception as e:
print(f"Failed to connect to {router['hostname']}: {e}")
def main():
routers = load_router_details('routers.yaml')['routers']
for router in routers:
connect_to_router(router)
if __name__ == "__main__":
main()