forked from AlexMunoz905/Python-Cisco-Backup
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultivendor_run.py
212 lines (199 loc) · 8.29 KB
/
multivendor_run.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Imports all the crucial items.
# All pre-installed besides Netmiko.
from csv import reader
from datetime import date, datetime
from netmiko import ConnectHandler
from ping3 import ping, verbose_ping
import getpass
import os
import sys
import time
import cmd
#sys.tracebacklimit = 0
# Checks if the folder exists, if not, it creates it.
if not os.path.exists('backup-config'):
os.makedirs('backup-config')
# Current time and formats it to the North American time of Month, Day, and Year.
now = datetime.now()
dt_string = now.strftime("%m-%d-%Y_%H-%M")
# Gives us the information we need to connect to Cisco.
def get_saved_config_c(host, username, password, enable_secret):
cisco_ios = {
'device_type': 'cisco_ios',
'host': host,
'username': username,
'password': password,
'secret': enable_secret,
}
# Creates the connection to the device.
net_connect = ConnectHandler(**cisco_ios)
net_connect.enable()
# Gets the running configuration.
output = net_connect.send_command("show run")
# Gets and splits the hostname for the output file name.
hostname = net_connect.send_command("show conf | i hostname")
hostname = hostname.split()
hostname = hostname[1]
# Creates the file name, which is the hostname, and the date and time.
fileName = hostname + "_" + dt_string
# Creates the text file in the backup-config folder with the special name, and writes to it.
backupFile = open("backup-config/" + fileName + ".txt", "w+")
backupFile.write(output)
print("Outputted to " + fileName + ".txt")
# Gives us the information we need to connect to Juniper.
def get_saved_config_j(host, username, password,):
juniper = {
'device_type': 'juniper',
'host': host,
'username': username,
'password': password,
}
# Creates the connection to the device.
net_connect = ConnectHandler(**juniper)
net_connect.enable()
# Gets the running configuration.
output = net_connect.send_command("show conf | display set")
# Gets and splits the hostname for the output file name.
hostname = net_connect.send_command("show ver | match hostname")
hostname = hostname.split()
hostname = hostname[2]
# Creates the file name, which is the hostname, and the date and time.
fileName = hostname + "_" + dt_string
# Creates the text file in the backup-config folder with the special name, and writes to it.
backupFile = open("backup-config/" + fileName + ".txt", "w+")
backupFile.write(output)
print("Outputted to " + fileName + ".txt")
# Gives us the information we need to connect to VyOS.
def get_saved_config_v(host, username, password,):
vyos = {
'device_type': 'vyos',
'host': host,
'port': '22',
'username': username,
'password': password,
}
# Creates the connection to the device.
net_connect = ConnectHandler(**vyos)
net_connect.enable()
# Gets the running configuration.
output = net_connect.send_command("show conf comm")
# Gets and splits the hostname for the output file name.
hostname = net_connect.send_command("sh conf | grep host-name | awk {'print $2'}")
hostname = hostname.split()
hostname = hostname[0]
# Creates the file name, which is the hostname, and the date and time.
fileName = hostname + "_" + dt_string
# Creates the text file in the backup-config folder with the special name, and writes to it.
backupFile = open("backup-config/" + fileName + ".txt", "w+")
backupFile.write(output)
print("Outputted to " + fileName + ".txt")
# Gives us the information we need to connect to Huawei.
def get_saved_config_h(host, username, password,):
huawei = {
"device_type": "huawei",
'host': host,
'username': username,
'password': password,
}
# Creates the connection to the device.
net_connect = ConnectHandler (** huawei)
net_connect.enable()
# Gets the running configuration.
output = net_connect.send_command("dis current-configuration")
# Gets and splits the hostname for the output file name.
hostname = net_connect.send_command("dis saved-configuration | inc sysname")
hostname = hostname.split()
hostname = hostname[1]
# Creates the file name, which is the hostname, and the date and time.
fileName = hostname + "_" + dt_string
# Creates the text file in the backup-config folder with the special name, and writes to it.
backupFile = open("backup-config/" + fileName + ".txt", "w+")
backupFile.write(output)
print("Outputted to " + fileName + ".txt")
# Gets the CSV file name for Cisco, and grabs the information from it.
def csv_option_c():
csv_name = input("\nWhat is the name of your CSV file for Cisco devices?: ")
with open(csv_name, 'r') as read_obj:
csv_reader = reader(read_obj)
list_of_rows = list(csv_reader)
rows = len(list_of_rows)
while rows >= 2:
rows = rows - 1
ip = list_of_rows[rows][0]
ip_ping = ping(ip)
if ip_ping == None:
fileName = "down_Cisco_Devices_" + dt_string + ".txt"
downDeviceOutput = open("backup-config/" + fileName, "a")
downDeviceOutput.write(str(ip) + "\n")
print(str(ip) + " is down!")
else:
get_saved_config_c(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2], list_of_rows[rows][3])
# Gets the CSV file name for Juniper, and grabs the information from it.
def csv_option_j():
csv_name = input("\nWhat is the name of your CSV file for Juniper device?: ")
with open(csv_name, 'r') as read_obj:
csv_reader = reader(read_obj)
list_of_rows = list(csv_reader)
rows = len(list_of_rows)
while rows >= 2:
rows = rows - 1
ip = list_of_rows[rows][0]
ip_ping = ping(ip)
if ip_ping == None:
fileName = "down_Juniper_Devices_" + dt_string + ".txt"
downDeviceOutput = open("backup-config/" + fileName, "a")
downDeviceOutput.write(str(ip) + "\n")
print(str(ip) + " is down!")
else:
get_saved_config_j(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)
# Gets the CSV file name for VyOS, and grabs the information from it.
def csv_option_v():
csv_name = input("\nWhat is the name of your CSV file for VyOS routers?: ")
with open(csv_name, 'r') as read_obj:
csv_reader = reader(read_obj)
list_of_rows = list(csv_reader)
rows = len(list_of_rows)
while rows >= 2:
rows = rows - 1
ip = list_of_rows[rows][0]
ip_ping = ping(ip)
if ip_ping == None:
fileName = "down_VyOS_Devices_" + dt_string + ".txt"
downDeviceOutput = open("backup-config/" + fileName, "a")
downDeviceOutput.write(str(ip) + "\n")
print(str(ip) + " is down!")
else:
get_saved_config_v(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)
# Gets the CSV file name for Huawei, and grabs the information from it.
def csv_option_h():
csv_name = input("\nWhat is the name of your CSV file for Huawei boxes?: ")
with open(csv_name, 'r') as read_obj:
csv_reader = reader(read_obj)
list_of_rows = list(csv_reader)
rows = len(list_of_rows)
while rows >= 2:
rows = rows - 1
ip = list_of_rows[rows][0]
ip_ping = ping(ip)
if ip_ping == None:
fileName = "down_Huawei_boxes_" + dt_string + ".txt"
downDeviceOutput = open("backup-config/" + fileName, "a")
downDeviceOutput.write(str(ip) + "\n")
print(str(ip) + " is down!")
else:
get_saved_config_h(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)
# Asks the user what option they are going to use.
print("\n1. Backup Cisco IOS devices.")
print("2. Backup Juniper devices.")
print("3. Backup VyOS routers.")
print("4. Backup Huawei boxes.\n")
choice = input("Please pick an option: ")
# This basically runs the whole file.
if choice == "1":
csv_option_c()
elif choice == "2":
csv_option_j()
elif choice == "3":
csv_option_v()
elif choice == "4":
csv_option_h()