-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
111 lines (96 loc) · 4.06 KB
/
test.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from netmiko import ConnectHandler
import os
from datetime import date, datetime
from ping3 import ping
import getpass
import click
class test():
def __init__(self,
username,
password):
self.username = username
self.password = password
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()
test.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)
test.device_commands(hostname, SSH)
# Push the commands stored into the "Commands.txt" file and store the output in different files called with Device hostname and timestamp
def device_commands(device_name, device_SSH):
with open(device_name + "_" + dt_string_full + ".txt", "w") as txt_file:
print("The file called: " + device_name + ".txt has been created into the " + backup_folder + "/" + dt_string + " folder. You will find here all the outputs. I'm going to close the SSH session")
for command in commands_list:
txt_file.write("######## " + command + " ######## \n\n")
output = (device_SSH.send_command(command))
txt_file.write(output + "\n\n\n\n")
device_SSH.disconnect()
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(self):
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.")
test.device_login(ip, self.username, self.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"
test.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")
test.create_folder(dt_string)
# Code
@click.command()
@click.option('--username',
prompt="AD Username",
help="AD Username",
required=True,envvar="USERNAME")
@click.option('--password',
prompt="AD Password",
help="AD Password",
required=True, hide_input=True,envvar="PASSWORD")
def cli(username,password):
invoke_class = test(username,password)
invoke_class.ip_reachability()
if __name__ == "__main__":
cli()