-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlb-config.py
133 lines (108 loc) · 3.64 KB
/
lb-config.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
#!/usr/local/bin/python3
import csv
import datetime
from getpass import getpass
from netmiko import ConnLogOnly
# --- Set the variables
sw_name = ()
sw_lb0 = ()
sw_lb1 = ()
sw_lb1sec = ()
command1 = ()
command2 = ()
command3 = ()
config_commands = []
# --- yes/no question
def confirm(prompt=None, resp=False):
"""prompts for yes or no response from the user. Returns True for yes and
False for no.
'resp' should be set to the default value assumed by the caller when
user simply types ENTER.
>>> confirm(prompt='Create Directory?', resp=True)
Create Directory? [y]|n:
True
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y:
False
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y: y
True
"""
if prompt is None:
prompt = 'Confirm'
if resp:
prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
else:
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = input(prompt)
if not ans:
return resp
if ans not in ['y', 'Y', 'n', 'N']:
print('please enter y or n.')
continue
if ans == 'y' or ans == 'Y':
return True
if ans == 'n' or ans == 'N':
return False
# --- Ask the credentials
username = input('Please insert your Nexus username: ')
print("And your password")
password = getpass()
with open("./lb-config.csv", 'r') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',')
for row in csv_reader:
sw_name = row[0]
sw_lb0 = row[1]
sw_lb1 = row[2]
sw_lb1sec = row[3]
print(40*"-")
print("From CSV file:")
print("Switch Name: ",sw_name)
print("Loopback0: ", sw_lb0)
print("Loopback1: ", sw_lb1)
print("Loopback1-Secondary: ", sw_lb1sec)
print(40*"-")
# --- Set the config commands - This can be customized
command1 = "ip address "+sw_lb0
command2 = "ip address "+sw_lb1
command3 = "ip address "+sw_lb1sec
config_commands = ["interface loopback0", command1, "interface loopback1", command2, command3]
# --- Show the switch and config commands:
print("Configuration changes for switch:", sw_name)
print(config_commands)
print(40*"-")
# --- Ask a confirmation before the config, if not, we skip this switch
prompt = str("Please confirm configuration change for switch: "+sw_name)
if confirm(prompt=prompt, resp=False) == True:
# --- Prepare the log file
now = datetime.datetime.now()
LOGFILE = "./logs/"+str(now.strftime("%Y%m%d-%H-%M_"))+str(sw_name)+".txt"
# --- Make the change
device = {
"device_type": "cisco_nxos",
"host": sw_name,
"username": username,
"password": password,
"log_file": LOGFILE,
"fast_cli" : True,
"verbose": False,
}
net_connect = ConnLogOnly(**device)
if net_connect is None:
print(sw_name+ ": Logging in failed... skipping")
print(40*"-")
else:
print(net_connect.find_prompt())
output = net_connect.send_config_set(config_commands)
print(output)
print()
net_connect.save_config()
print("Changes done, logged and config saved")
print(40*"-")
net_connect.disconnect()
# --- All done
print(40*"=")
print("All changes are done")
print(40*"=")
raise SystemExit