-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmultivendor_run.py
65 lines (60 loc) · 3.07 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
# All pre-installed besides Netmiko and ping3.
from csv import reader
from datetime import datetime
from netmiko import ConnectHandler
from ping3 import ping, verbose_ping
from vendor_backups import cisco_ios,cisco_asa,fortinet,huawei,juniper,microtik,vyos
import os
# Specified CSV file for the script to grab the hosts from.
csv_name = "backup_hosts.csv"
# 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")
# Main function.
def run_script(user_selection):
# Imports the CSV file specified in the csv_name variable.
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]
# Pings the hosts in the CSV file, successful pings move onto the else statement.
# Unsuccessful pings go into a down_devices file.
ip_ping = ping(ip)
if ip_ping == None:
fileName = "down_devices_" + dt_string + ".txt"
downDeviceOutput = open("backup-config/" + fileName, "a")
downDeviceOutput.write(str(ip) + "\n")
print(str(ip) + " is down!")
else:
# Based on user selection, run the script in the vendor_backups folder. The passed variables are hosts, username, password, and optional secret.
if user_selection == "1":
cisco_ios.backup(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2], list_of_rows[rows][3])
elif user_selection == "2":
cisco_asa.backup(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2], list_of_rows[rows][3])
elif user_selection == "3":
juniper.backup(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2])
elif user_selection == "4":
vyos.backup(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2])
elif user_selection == "5":
huawei.backup(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2])
elif user_selection == "6":
fortinet.backup(list_of_rows[rows][0], list_of_rows[rows][1], list_of_rows[rows][2])
elif user_selection == "7":
microtik.backup(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 Cisco ASA devices.")
print("3. Backup Juniper devices.")
print("4. Backup VyOS routers.")
print("5. Backup Huawei boxes.")
print("6. Backup Fortinet devices.")
print("7. Backup MicroTik devices.\n")
user_selection = input("Please pick an option: ")
# Pass the users choice to the main function.
run_script(user_selection)