-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (55 loc) · 2.13 KB
/
main.py
File metadata and controls
62 lines (55 loc) · 2.13 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
58
59
60
61
62
import os
import datetime
import yaml
from netmiko import (
ConnectHandler,
NetmikoTimeoutException,
NetmikoAuthenticationException,
)
class functions:
def __init__(self,device,commands):
self.device = device
self.commands = commands
def send_show_command(self):# function to perform show commands
try:
with ConnectHandler(**self.device) as ssh:
ssh.enable()
for command in self.commands:
output = ssh.send_command(command)
t = datetime.datetime.now().strftime("%d.%m.%Y - %H;%M;%S")
with open("show_" + str(t) + ".txt", "w") as file:
file.write(output)
path = os.getcwd()
os.startfile(path + "\\" + file.name)
return output
except (NetmikoTimeoutException, NetmikoAuthenticationException) as error:
print(error)
def send_config(self): # function to add new configurations to devices
try:
with ConnectHandler(**self.device) as ssh:
ssh.enable()
output = ssh.send_config_set(self.commands)
t = datetime.datetime.now().strftime("%d.%m.%Y - %H;%M;%S")
with open("config_" + str(t) + ".txt", "w") as file:
file.write(output)
path = os.getcwd()
os.startfile(path + "\\" + file.name)
return output
except (NetmikoTimeoutException, NetmikoAuthenticationException) as error:
print(error)
def inventory(): #loads information from devices
try:
with open("devices.yaml") as f:
devices = f.read()
inventory_dict = yaml.safe_load(devices)
return inventory_dict
except:
print("Error while loading inventory from file!")
def config(): #loads device configurations file
try:
with open("config.yaml") as f:
config = f.read()
config_list = yaml.safe_load(config)
return config_list
except:
print("Error while loading configs from file!")