1+ from csv import reader
2+ from datetime import datetime
3+ from netmiko import ConnectHandler
4+ from ping3 import ping , verbose_ping
5+ from vendor_backups import cisco ,fortinet ,huawei ,juniper ,microtik ,vyos
6+ import os
7+ from tkinter import *
8+ from tkinter .ttk import *
9+ from tkinter .filedialog import *
10+ from tkinter import messagebox
11+
12+ # Initializes Tkinter
13+ root = Tk ()
14+ root .title ("Backup Configurator" )
15+ tk_frame = Frame (root )
16+ tk_frame .pack (expand = True )
17+
18+ # Checks if the folder exists, if not, it creates it.
19+ if not os .path .exists ('backup-config' ):
20+ os .makedirs ('backup-config' )
21+
22+ # Current time and formats it to the North American time of Month, Day, and Year.
23+ now = datetime .now ()
24+ dt_string = now .strftime ("%m-%d-%Y_%H-%M" )
25+
26+ # Popup window that tells that the backup worked
27+ def backup_completion_popup (backup_file_name ):
28+ messagebox .showinfo ("Configuration Backup" ,"Saved To: " + backup_file_name )
29+
30+ # Popup window that tells user each down host
31+ def down_host_popup (down_host_ip ):
32+ messagebox .showinfo ("Down Host" ,"Down Host: " + down_host_ip )
33+
34+ # Main function.
35+ def run_script (user_selection ):
36+ file_path = askopenfilename (title = "Open CSV File" , filetypes = [("CSV files" , "*.csv" )])
37+ if file_path :
38+ with open (file_path , 'r' ) as read_obj :
39+ csv_reader = reader (read_obj )
40+ list_of_rows = list (csv_reader )
41+ rows = len (list_of_rows )
42+ while rows >= 2 :
43+ rows = rows - 1
44+ ip = list_of_rows [rows ][0 ]
45+ # Pings the hosts in the CSV file, successful pings move onto the else statement.
46+ # Unsuccessful pings go into a down_devices file.
47+ ip_ping = ping (ip )
48+ if ip_ping == None :
49+ fileName = "down_devices_" + dt_string + ".txt"
50+ downDeviceOutput = open ("backup-config/" + fileName , "a" )
51+ downDeviceOutput .write (str (ip ) + "\n " )
52+ down_host_popup (str (ip ))
53+ else :
54+ # Based on user selection, run the script in the vendor_backups folder. The passed variables are hosts, username, password, and optional secret.
55+ if user_selection == "1" :
56+ cisco .backup (list_of_rows [rows ][0 ], list_of_rows [rows ][1 ], list_of_rows [rows ][2 ], list_of_rows [rows ][3 ])
57+ backup_completion_popup (cisco .gui_filename_output )
58+ elif user_selection == "2" :
59+ juniper .backup (list_of_rows [rows ][0 ], list_of_rows [rows ][1 ], list_of_rows [rows ][2 ])
60+ backup_completion_popup (juniper .gui_filename_output )
61+ elif user_selection == "3" :
62+ vyos .backup (list_of_rows [rows ][0 ], list_of_rows [rows ][1 ], list_of_rows [rows ][2 ])
63+ backup_completion_popup (vyos .gui_filename_output )
64+ elif user_selection == "4" :
65+ huawei .backup (list_of_rows [rows ][0 ], list_of_rows [rows ][1 ], list_of_rows [rows ][2 ])
66+ backup_completion_popup (huawei .gui_filename_output )
67+ elif user_selection == "5" :
68+ fortinet .backup (list_of_rows [rows ][0 ], list_of_rows [rows ][1 ], list_of_rows [rows ][2 ])
69+ backup_completion_popup (fortinet .gui_filename_output )
70+ elif user_selection == "6" :
71+ microtik .backup (list_of_rows [rows ][0 ], list_of_rows [rows ][1 ], list_of_rows [rows ][2 ])
72+ backup_completion_popup (microtik .gui_filename_output )
73+
74+ # Build the button and assign values
75+ tk_cisco = Button (tk_frame , text = "Cisco" , command = lambda : run_script ("1" ))
76+ tk_juniper = Button (tk_frame , text = "Juniper" , command = lambda : run_script ("2" ))
77+ tk_vyos = Button (tk_frame , text = "VyOS" , command = lambda : run_script ("3" ))
78+ tk_huawei = Button (tk_frame , text = "Huawei" , command = lambda : run_script ("4" ))
79+ tk_fortinet = Button (tk_frame , text = "Fortinet" , command = lambda : run_script ("5" ))
80+ tk_microtik = Button (tk_frame , text = "Microtik" , command = lambda : run_script ("6" ))
81+
82+ # Place the button on the GUI
83+ tk_cisco .pack (side = LEFT , padx = 5 , fill = BOTH , expand = True )
84+ tk_juniper .pack (side = LEFT , padx = 5 , fill = BOTH , expand = True )
85+ tk_vyos .pack (side = LEFT , padx = 5 , fill = BOTH , expand = True )
86+ tk_huawei .pack (side = LEFT , padx = 5 , fill = BOTH , expand = True )
87+ tk_fortinet .pack (side = LEFT , padx = 5 , fill = BOTH , expand = True )
88+ tk_microtik .pack (side = LEFT , padx = 5 , fill = BOTH , expand = True )
89+
90+ # Runs the gui
91+ mainloop ()
0 commit comments