-
Notifications
You must be signed in to change notification settings - Fork 0
/
SYS_UP.py
136 lines (101 loc) · 4.8 KB
/
SYS_UP.py
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
from netmiko import ConnectHandler, NetMikoAuthenticationException , NetmikoTimeoutException, NetmikoBaseException
from prettytable import PrettyTable
import yaml
class Inventory_db:
def __init__(self):
self.hosts_IOS = {}
self.hosts_IOS_XR ={}
self.hosts_NOKIA_SROS= {}
external_data = self.get_external_data()
self.username = external_data["user_pass"]["my_username"]
self.my_pass = external_data["user_pass"]["my_password"]
self.hosts_IOS["IOS"] = external_data["IOS_IOS_XE"]
self.hosts_IOS_XR["IOS_XR"] = external_data["IOS_XR"]
self.hosts_NOKIA_SROS["NOKIA_SROS"] = external_data["NOKIA_SROS"]
def get_external_data(self):
host_info = "host_info.yaml"
with open ("host_info.yaml") as file:
data = yaml.safe_load(file)
return data
class Sys_up:
def __init__(self):
self.inventory = Inventory_db()
def host_connect_parameters(self, hostname, os_type):
dev_params = {
"device_type": os_type,
"host": hostname,
"username": self.inventory.username,
"password": self.inventory.my_pass
}
return dev_params
def connect_to_dev(self, hosts, comand, dev_os):
hostinf={}
host_sys_info = []
for host in hosts:
print("Gathering data on {}".format(host))
host_params = self.host_connect_parameters(host, dev_os)
try:
with ConnectHandler(**host_params) as dev_connect:
result = dev_connect.send_command(comand, use_textfsm=True)
#print(result)
host_sys_info.append(result[0])
except NetMikoAuthenticationException:
print("WARNING!!! AUTHENTICATION FAILED - DEVICE: {} ".format(host))
except NetmikoTimeoutException:
print("WARNING!!! DEVICE {} NOT REACHABLE OR SSH NOT ENABLED".format(host))
return host_sys_info
# for devices with non use_textfsm support
def tl_dev_output(self, hosts, sros_command, dev_os, key_comand_ref="result"):
per_dev_output_collectionnfo = []
try:
for host in hosts:
output_collection = {}
print("Gathering data on {}".format(host))
dev = self.host_connect_parameters(host, dev_os)
with ConnectHandler(**dev) as dev_connect:
dev_output = dev_connect.send_command(sros_command)
output_collection["hostname"] = host
output_collection[key_comand_ref] = dev_output.split(" : ")[1].strip("sec")
per_dev_output_collectionnfo.append(output_collection)
except NetMikoAuthenticationException:
print("WARNING!!! AUTHENTICATION FAILED - DEVICE: {} ".format(host))
except NetmikoTimeoutException:
print("WARNING!!! DEVICE {} NOT REACHABLE OR SSH NOT ENABLED".format(host))
return per_dev_output_collectionnfo
def global_dev_info(self, hosts, comand, sros_command="", key_comand_ref="result"):
total_dev_groups = len(hosts)
host_details = []
for x_element in range(total_dev_groups):
for k , v in hosts[x_element].items():
if k == "NOKIA_SROS":
if v == None or v == False:
continue
else:
dev_os = "nokia_srl"
_NOKIA_conect_details = self.tl_dev_output(v, sros_command,dev_os, key_comand_ref=key_comand_ref )
host_details += _NOKIA_conect_details
else:
if v == None or v == False:
continue
else:
dev_os = "cisco_ios"
host_details += self.connect_to_dev(v,comand, dev_os)
return host_details
def sys_up_table(self, devs_grp):
table_view = PrettyTable(["DEVICES", "UPTIME"])
devs_info = self.global_dev_info(devs_grp, "show version", sros_command="show uptime", key_comand_ref="uptime")
if len(devs_info) > 0:
print("")
print(" UPTIME PER DEVICE - SUMMARY VIEW ")
for info in devs_info:
hostname = info["hostname"]
uptime = info["uptime"]
table_view.add_row([hostname, uptime])
return table_view
else:
print("Device info not found, please check ssh and devices reachability")
app = Sys_up()
sys_up_view = app.sys_up_table([app.inventory.hosts_IOS_XR, app.inventory.hosts_IOS, app.inventory.hosts_NOKIA_SROS])
print(sys_up_view, "\n")
print("MIT License")
print("Copyright (c) 2023 ThierryLab.com")