-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatic_show_commands_append.py
90 lines (78 loc) · 3.32 KB
/
static_show_commands_append.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from netmiko import ConnectHandler
import os
from datetime import date, datetime
from ping3 import ping
import getpass
def create_folder(folder):
if not os.path.exists(folder):
os.mkdir(folder)
os.chdir(folder)
# Login to the devices
def device_login(host, username, password):
cisco_ios = {
'device_type': 'cisco_ios',
'host': host,
'username': username,
'password': password,
}
try:
connection = ConnectHandler(**cisco_ios)
connection.enable()
device_hostname(connection)
except Exception as err:
print(f"Oops! {err}")
# Retrieve the device Hostname
def device_hostname(SSH):
output = SSH.send_command("show run")
dev_name = SSH.send_command("show run | in hostname")
dev_name = dev_name.split(" ")
hostname = dev_name[1]
print("The device name is: ", hostname)
device_commands(hostname, SSH)
# Push the commands stored into the "Commands.txt" file and store the output in an unique file called with the actual date
def device_commands(device_name, device_SSH):
txt_file = open(dt_string_full + ".txt", "a")
txt_file.write("#####" + device_name + "#####" + device_name + "#####" + device_name + "#####" + device_name + "#####" + device_name + "\n")
for command in commands_list:
txt_file.write("######## " + command + " ######## \n\n")
output = (device_SSH.send_command(command))
txt_file.write(output + "\n\n")
device_SSH.disconnect()
SSH_Check_Connection = device_SSH.is_alive()
txt_file.write("\n\n\n\n\n\n\n\n ######################################################################################## \n\n\n\n\n\n\n\n")
print("\n\n\n")
# Verify if an IP address is reachable.
# If YES: run "device_login" function
# If NO: Create a file called with the IP Address of the device and write inside "This device is not reachable. Please verify its status and connectivity"
def ip_reachability():
for ip in ip_list:
print("Testing: " + ip)
ip_reach = ping(ip)
if ip_reach == None:
print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n")
with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice:
downdevice.write("This device is not reachable. Please verify its status and connectivity")
else:
print ("It's reachable! I'm going to retrieve data from it.")
device_login(ip, username, password)
# User enters device credentials
username = input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
# Code opens the file where IP Addresses are stored (1 per line)
file_ip = open("IPAddressList.txt", "r")
total_ips = file_ip.read()
ip_list = total_ips.splitlines()
# Code opens the file where commands are stored (1 per line)
file_commands = open("Commands.txt", "r")
total_commands = file_commands.read()
commands_list = total_commands.splitlines()
# Create Backup Folder
backup_folder = "BACKUP"
create_folder(backup_folder)
# Create daily folder inside Backup Folder
now = datetime.now()
dt_string = now.strftime("%m-%d-%Y")
dt_string_full = now.strftime("%m-%d-%Y_%H-%M-%S")
create_folder(dt_string)
# Code
ip_reachability()