-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_ssh_connectivity.py
More file actions
57 lines (50 loc) · 1.74 KB
/
test_ssh_connectivity.py
File metadata and controls
57 lines (50 loc) · 1.74 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
This Script verifies SSH connectivity to all IOS-XE and IOS-XR routers.
"""
import yaml
from netmiko import ConnectHandler
from colorama import Fore, Style, init
# Initialize colorama
init(autoreset=True)
def load_devices():
"""Loads device credentials from devices.yaml."""
try:
with open("devices.yaml", "r") as file:
data = yaml.safe_load(file)
if "devices" not in data:
raise KeyError("Missing 'devices' key in YAML file")
return data["devices"]
except yaml.YAMLError as e:
print(Fore.RED + f"❌ YAML Parsing Error: {e}")
exit(1)
except FileNotFoundError:
print(Fore.RED + "❌ Error: devices.yaml file not found.")
exit(1)
def ssh_connect(device):
"""
Attempts SSH connection to a device.
Args:
device (dict): Device connection details.
Returns:
bool: True if SSH is successful, False otherwise.
"""
try:
connection = ConnectHandler(
device_type="cisco_ios" if device["os_type"] == "ios-xe" else "cisco_xr",
host=device["host"],
username=device["username"],
password=device["password"],
port=device["port"]
)
connection.disconnect()
print(Fore.GREEN + f"✅ SUCCESS: SSH to {device['name']} ({device['host']})")
return True
except Exception as e:
print(Fore.RED + f"❌ FAILED: SSH to {device['name']} ({device['host']}) - {e}")
return False
if __name__ == "__main__":
devices = load_devices()
print(Fore.CYAN + "\n🔹 Attempting SSH connections...\n" + Style.RESET_ALL)
for device in devices:
ssh_connect(device)
print(Fore.YELLOW + "\n🔹 SSH Checks Completed.\n")