Skip to content

Commit f399c9d

Browse files
committed
Merge branch 'master' of github.com:AlexMunoz905/Python-Cisco-Backup
2 parents f3f2f78 + ac31d7a commit f399c9d

9 files changed

Lines changed: 121 additions & 9 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ backup-config
22
.idea/
33
dev_hosts.csv
44
vendor_backups/__pycache__
5-
__pycache__
5+
__pycache__
6+
.vscode
7+
dist
8+
build

gui.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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()

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
netmiko>=3.3.3
2-
ping3>=2.7.0
1+
netmiko>=4.3.0
2+
ping3>=4.0.8

vendor_backups/cisco.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ def backup(host, username, password, enable_secret):
3232
# Creates the text file in the backup-config folder with the special name, and writes to it.
3333
backupFile = open("backup-config/" + fileName + ".txt", "w+")
3434
backupFile.write(output)
35-
print("Outputted to " + fileName + ".txt")
35+
print("Outputted to " + fileName + ".txt")
36+
# For the GUI
37+
global gui_filename_output
38+
gui_filename_output = fileName

vendor_backups/fortinet.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ def backup(host, username, password):
2929
# Creates the text file in the backup-config folder with the special name, and writes to it.
3030
backupFile = open("backup-config/" + fileName + ".txt", "w+")
3131
backupFile.write(output)
32-
print("Outputted to " + fileName + ".txt!")
32+
print("Outputted to " + fileName + ".txt!")
33+
# For the GUI
34+
global gui_filename_output
35+
gui_filename_output = fileName

vendor_backups/huawei.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ def backup(host, username, password):
2929
# Creates the text file in the backup-config folder with the special name, and writes to it.
3030
backupFile = open("backup-config/" + fileName + ".txt", "w+")
3131
backupFile.write(output)
32-
print("Outputted to " + fileName + ".txt")
32+
print("Outputted to " + fileName + ".txt")
33+
# For the GUI
34+
global gui_filename_output
35+
gui_filename_output = fileName

vendor_backups/juniper.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ def backup(host, username, password,):
2929
# Creates the text file in the backup-config folder with the special name, and writes to it.
3030
backupFile = open("backup-config/" + fileName + ".txt", "w+")
3131
backupFile.write(output)
32-
print("Outputted to " + fileName + ".txt")
32+
print("Outputted to " + fileName + ".txt")
33+
# For the GUI
34+
global gui_filename_output
35+
gui_filename_output = fileName

vendor_backups/microtik.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ def backup(host, username, password):
2828
# Creates the text file in the backup-config folder with the special name, and writes to it.
2929
backupFile = open("backup-config/" + fileName + ".txt", "w+")
3030
backupFile.write(output)
31-
print("Outputted to " + fileName + ".txt")
31+
print("Outputted to " + fileName + ".txt")
32+
# For the GUI
33+
global gui_filename_output
34+
gui_filename_output = fileName

vendor_backups/vyos.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ def backup(host, username, password):
2929
# Creates the text file in the backup-config folder with the special name, and writes to it.
3030
backupFile = open("backup-config/" + fileName + ".txt", "w+")
3131
backupFile.write(output)
32-
print("Outputted to " + fileName + ".txt")
32+
print("Outputted to " + fileName + ".txt")
33+
# For the GUI
34+
global gui_filename_output
35+
gui_filename_output = fileName

0 commit comments

Comments
 (0)