Skip to content

Commit 2747b2d

Browse files
authored
Add files via upload
1 parent 95b75b0 commit 2747b2d

File tree

3 files changed

+332
-0
lines changed

3 files changed

+332
-0
lines changed

multivendor_run.py

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
# Imports all the crucial items.
2+
# All pre-installed besides Netmiko.
3+
from csv import reader
4+
from datetime import date, datetime
5+
from netmiko import ConnectHandler
6+
from ping3 import ping, verbose_ping
7+
import getpass
8+
import os
9+
import sys
10+
import time
11+
import cmd
12+
13+
#sys.tracebacklimit = 0
14+
15+
# Checks if the folder exists, if not, it creates it.
16+
if not os.path.exists('backup-config'):
17+
os.makedirs('backup-config')
18+
19+
# Current time and formats it to the North American time of Month, Day, and Year.
20+
now = datetime.now()
21+
dt_string = now.strftime("%m-%d-%Y_%H-%M")
22+
23+
# Gives us the information we need to connect to Cisco.
24+
def get_saved_config_c(host, username, password, enable_secret):
25+
cisco_ios = {
26+
'device_type': 'cisco_ios',
27+
'host': host,
28+
'username': username,
29+
'password': password,
30+
'secret': enable_secret,
31+
}
32+
# Creates the connection to the device.
33+
net_connect = ConnectHandler(**cisco_ios)
34+
net_connect.enable()
35+
# Gets the running configuration.
36+
output = net_connect.send_command("show run")
37+
# Gets and splits the hostname for the output file name.
38+
hostname = net_connect.send_command("show conf | i hostname")
39+
hostname = hostname.split()
40+
hostname = hostname[1]
41+
# Creates the file name, which is the hostname, and the date and time.
42+
fileName = hostname + "_" + dt_string
43+
# Creates the text file in the backup-config folder with the special name, and writes to it.
44+
backupFile = open("backup-config/" + fileName + ".txt", "w+")
45+
backupFile.write(output)
46+
print("Outputted to " + fileName + ".txt")
47+
48+
# Gives us the information we need to connect to Juniper.
49+
def get_saved_config_j(host, username, password,):
50+
juniper = {
51+
'device_type': 'juniper',
52+
'host': host,
53+
'username': username,
54+
'password': password,
55+
}
56+
# Creates the connection to the device.
57+
net_connect = ConnectHandler(**juniper)
58+
net_connect.enable()
59+
# Gets the running configuration.
60+
output = net_connect.send_command("show conf | display set")
61+
# Gets and splits the hostname for the output file name.
62+
hostname = net_connect.send_command("show ver | i hostname")
63+
hostname = hostname.split()
64+
hostname = hostname[1]
65+
# Creates the file name, which is the hostname, and the date and time.
66+
fileName = hostname + "_" + dt_string
67+
# Creates the text file in the backup-config folder with the special name, and writes to it.
68+
backupFile = open("backup-config/" + fileName + ".txt", "w+")
69+
backupFile.write(output)
70+
print("Outputted to " + fileName + ".txt")
71+
72+
# Gives us the information we need to connect to VyOS.
73+
def get_saved_config_v(host, username, password,):
74+
vyos = {
75+
'device_type': 'vyos',
76+
'host': host,
77+
'port': '21234',
78+
'username': username,
79+
'password': password,
80+
}
81+
# Creates the connection to the device.
82+
net_connect = ConnectHandler(** vyos)
83+
net_connect.enable()
84+
# Gets the running configuration.
85+
output = net_connect.send_command("show conf comm")
86+
# Gets and splits the hostname for the output file name.
87+
hostname = net_connect.send_command("sh conf | grep host-name | awk {'print $2'}")
88+
hostname = hostname.split()
89+
hostname = hostname[0]
90+
# Creates the file name, which is the hostname, and the date and time.
91+
fileName = hostname + "_" + dt_string
92+
# Creates the text file in the backup-config folder with the special name, and writes to it.
93+
backupFile = open("backup-config/" + fileName + ".txt", "w+")
94+
backupFile.write(output)
95+
print("Outputted to " + fileName + ".txt")
96+
97+
# Gives us the information we need to connect to Huawei.
98+
def get_saved_config_h(host, username, password,):
99+
huawei = {
100+
"device_type": "huawei",
101+
'host': host,
102+
'username': username,
103+
'password': password,
104+
}
105+
# Creates the connection to the device.
106+
net_connect = ConnectHandler (** huawei)
107+
net_connect.enable()
108+
# Gets the running configuration.
109+
output = net_connect.send_command("dis current-configuration")
110+
# Gets and splits the hostname for the output file name.
111+
hostname = net_connect.send_command("dis saved-configuration | inc sysname")
112+
hostname = hostname.split()
113+
hostname = hostname[1]
114+
# Creates the file name, which is the hostname, and the date and time.
115+
fileName = hostname + "_" + dt_string
116+
# Creates the text file in the backup-config folder with the special name, and writes to it.
117+
backupFile = open("backup-config/" + fileName + ".txt", "w+")
118+
backupFile.write(output)
119+
print("Outputted to " + fileName + ".txt")
120+
121+
# Gets the CSV file name for Cisco, and grabs the information from it.
122+
def csv_option_c():
123+
csv_name = input("\nWhat is the name of your CSV file for Cisco devices?: ")
124+
with open(csv_name, 'r') as read_obj:
125+
csv_reader = reader(read_obj)
126+
list_of_rows = list(csv_reader)
127+
rows = len(list_of_rows)
128+
while rows >= 2:
129+
rows = rows - 1
130+
ip = list_of_rows[rows][0]
131+
ip_ping = ping(ip)
132+
if ip_ping == None:
133+
fileName = "down_Cisco_Devices_" + dt_string + ".txt"
134+
downDeviceOutput = open("backup-config/" + fileName, "a")
135+
downDeviceOutput.write(str(ip) + "\n")
136+
print(str(ip) + " is down!")
137+
else:
138+
get_saved_config_c(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2], list_of_rows[rows][3])
139+
140+
# Gets the CSV file name for Juniper, and grabs the information from it.
141+
def csv_option_j():
142+
csv_name = input("\nWhat is the name of your CSV file for Juniper device?: ")
143+
with open(csv_name, 'r') as read_obj:
144+
csv_reader = reader(read_obj)
145+
list_of_rows = list(csv_reader)
146+
rows = len(list_of_rows)
147+
while rows >= 2:
148+
rows = rows - 1
149+
ip = list_of_rows[rows][0]
150+
ip_ping = ping(ip)
151+
if ip_ping == None:
152+
fileName = "down_Juniper_Devices_" + dt_string + ".txt"
153+
downDeviceOutput = open("backup-config/" + fileName, "a")
154+
downDeviceOutput.write(str(ip) + "\n")
155+
print(str(ip) + " is down!")
156+
else:
157+
get_saved_config_j(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)
158+
159+
# Gets the CSV file name for VyOS, and grabs the information from it.
160+
def csv_option_v():
161+
csv_name = input("\nWhat is the name of your CSV file for VyOS routers?: ")
162+
with open(csv_name, 'r') as read_obj:
163+
csv_reader = reader(read_obj)
164+
list_of_rows = list(csv_reader)
165+
rows = len(list_of_rows)
166+
while rows >= 2:
167+
rows = rows - 1
168+
ip = list_of_rows[rows][0]
169+
ip_ping = ping(ip)
170+
if ip_ping == None:
171+
fileName = "down_VyOS_Devices_" + dt_string + ".txt"
172+
downDeviceOutput = open("backup-config/" + fileName, "a")
173+
downDeviceOutput.write(str(ip) + "\n")
174+
print(str(ip) + " is down!")
175+
else:
176+
get_saved_config_v(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)
177+
178+
# Gets the CSV file name for Huawei, and grabs the information from it.
179+
def csv_option_h():
180+
csv_name = input("\nWhat is the name of your CSV file for Huawei boxes?: ")
181+
with open(csv_name, 'r') as read_obj:
182+
csv_reader = reader(read_obj)
183+
list_of_rows = list(csv_reader)
184+
rows = len(list_of_rows)
185+
while rows >= 2:
186+
rows = rows - 1
187+
ip = list_of_rows[rows][0]
188+
ip_ping = ping(ip)
189+
if ip_ping == None:
190+
fileName = "down_Huawei_boxes_" + dt_string + ".txt"
191+
downDeviceOutput = open("backup-config/" + fileName, "a")
192+
downDeviceOutput.write(str(ip) + "\n")
193+
print(str(ip) + " is down!")
194+
else:
195+
get_saved_config_h(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)
196+
197+
# Asks the user what option they are going to use.
198+
print("\n1. Backup Cisco IOS devices.")
199+
print("2. Backup Juniper devices.")
200+
print("3. Backup VyOS routers.")
201+
print("4. Backup Huawei boxes.\n")
202+
choice = input("Please pick an option: ")
203+
204+
# This basically runs the whole file.
205+
if choice == "1":
206+
csv_option_c()
207+
elif choice == "2":
208+
csv_option_j()
209+
elif choice == "3":
210+
csv_option_v()
211+
elif choice == "4":
212+
csv_option_h()

ni_run_cisco.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# All pre-installed besides Netmiko.
2+
from csv import reader
3+
from datetime import date, datetime
4+
from netmiko import ConnectHandler
5+
from ping3 import ping, verbose_ping
6+
import getpass
7+
import os
8+
#import sys
9+
10+
#sys.tracebacklimit = 0
11+
12+
# Checks if the folder exists, if not, it creates it.
13+
if not os.path.exists('backup-config'):
14+
os.makedirs('backup-config')
15+
16+
# Current time and formats it to the North American time of Month, Day, and Year.
17+
now = datetime.now()
18+
dt_string = now.strftime("%m-%d-%Y_%H-%M")
19+
20+
# Gives us the information we need to connect.
21+
def get_saved_config(host, username, password, enable_secret):
22+
cisco_ios = {
23+
'device_type': 'cisco_ios',
24+
'host': host,
25+
'username': username,
26+
'password': password,
27+
'secret': enable_secret,
28+
'auth_timeout': '10,0',
29+
}
30+
# Creates the connection to the device.
31+
net_connect = ConnectHandler(**cisco_ios)
32+
net_connect.enable()
33+
# Gets the running configuration.
34+
output = net_connect.send_command("show run")
35+
# Gets and splits the hostname for the output file name.
36+
hostname = net_connect.send_command("show ver | i uptime")
37+
hostname = hostname.split()
38+
hostname = hostname[0]
39+
# Creates the file name, which is the hostname, and the date and time.
40+
fileName = hostname + "_" + dt_string
41+
# Creates the text file in the backup-config folder with the special name, and writes to it.
42+
backupFile = open("backup-config/" + fileName + ".txt", "w+")
43+
backupFile.write(output)
44+
print("Outputted to " + fileName + ".txt!")
45+
46+
# Gets the CSV file name, and grabs the information from it.
47+
with open('cisco_backup_hosts.csv') as csvfile:
48+
csv_reader = reader(csvfile)
49+
list_of_rows = list(csv_reader)
50+
rows = len(list_of_rows)
51+
while rows >= 2:
52+
rows = rows - 1
53+
ip = list_of_rows[rows][0]
54+
ip_ping = ping(ip)
55+
if ip_ping == None:
56+
fileName = "downDevices_" + dt_string + ".txt"
57+
downDeviceOutput = open("backup-config/" + fileName, "a")
58+
downDeviceOutput.write(str(ip) + "\n")
59+
print(str(ip) + " is down!")
60+
else:
61+
get_saved_config(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2], list_of_rows[rows][3])

ni_run_huawei.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# All pre-installed besides Netmiko.
2+
from csv import reader
3+
from datetime import date, datetime
4+
from netmiko import ConnectHandler
5+
from ping3 import ping, verbose_ping
6+
import getpass
7+
import os
8+
#import sys
9+
10+
#sys.tracebacklimit = 0
11+
12+
# Checks if the folder exists, if not, it creates it.
13+
if not os.path.exists('backup-config'):
14+
os.makedirs('backup-config')
15+
16+
# Current time and formats it to the North American time of Month, Day, and Year.
17+
now = datetime.now()
18+
dt_string = now.strftime("%m-%d-%Y_%H-%M")
19+
20+
# Gives us the information we need to connect.
21+
def get_saved_config(host, username, password,):
22+
huawei = {
23+
"device_type": "huawei",
24+
'ip': host,
25+
'username': username,
26+
'password': password,
27+
}
28+
# Creates the connection to the device.
29+
net_connect = ConnectHandler (** huawei)
30+
net_connect.enable()
31+
# Gets the running configuration.
32+
output = net_connect.send_command("dis current-configuration")
33+
# Gets and splits the hostname for the output file name.
34+
hostname = net_connect.send_command("dis saved-configuration | inc sysname")
35+
hostname = hostname.split()
36+
hostname = hostname[1]
37+
# Creates the file name, which is the hostname, and the date and time.
38+
fileName = hostname + "_" + dt_string
39+
# Creates the text file in the backup-config folder with the special name, and writes to it.
40+
backupFile = open("backup-config/" + fileName + ".txt", "w+")
41+
backupFile.write(output)
42+
print("Outputted to " + fileName + ".txt!")
43+
44+
# Gets the CSV file name, and grabs the information from it.
45+
with open('huawei_backup_hosts.csv') as csvfile:
46+
csv_reader = reader(csvfile)
47+
list_of_rows = list(csv_reader)
48+
rows = len(list_of_rows)
49+
while rows >= 2:
50+
rows = rows - 1
51+
ip = list_of_rows[rows][0]
52+
ip_ping = ping(ip)
53+
if ip_ping == None:
54+
fileName = "downDevices_" + dt_string + ".txt"
55+
downDeviceOutput = open("backup-config/" + fileName, "a")
56+
downDeviceOutput.write(str(ip) + "\n")
57+
print(str(ip) + " is down!")
58+
else:
59+
get_saved_config(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2],)

0 commit comments

Comments
 (0)