|
| 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() |
0 commit comments