This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect-sheduled-show-tech.py
executable file
·141 lines (125 loc) · 4.88 KB
/
collect-sheduled-show-tech.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""
This script collects all the tech-support files stored on Arista switches flash
"""
# Imports
import ssl
from socket import setdefaulttimeout
from getpass import getpass
import sys
from time import strftime, gmtime
from argparse import ArgumentParser
import os
import paramiko
from jsonrpclib import Server,jsonrpc
from scp import SCPClient
from tqdm import tqdm
PORT = 22
# pylint: disable=protected-access
ssl._create_default_https_context = ssl._create_unverified_context
date = strftime("%d %b %Y %H:%M:%S", gmtime())
def device_directories (device, root_dir):
cwd = os.getcwd()
show_tech_directory = cwd + '/' + root_dir
device_directory = show_tech_directory + '/' + device
for directory in [show_tech_directory, device_directory]:
if not os.path.exists(directory):
os.makedirs(directory)
result = show_tech_directory, device_directory
return result
def create_ssh_client (device, port, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(device, port, username, password)
return client
def main():
parser = ArgumentParser(
description='Collect all the tech-support files'
)
parser.add_argument(
'-i',
help='Text file containing a list of switches',
dest='file',
required=True
)
parser.add_argument(
'-u',
help='Devices username',
dest='username',
required=True
)
parser.add_argument(
'-o',
help='Output directory',
dest='output_directory',
required=True
)
args = parser.parse_args()
args.password = getpass(prompt='Device password: ')
try:
with open(args.file, 'r', encoding='utf8') as file:
devices = file.readlines()
except FileNotFoundError:
print('Error reading ' + args.file)
sys.exit(1)
for i,device in enumerate(devices):
devices[i] = device.strip()
# Remove unreachable devices from devices list
unreachable = []
print('Checking connectivity to devices .... please be patient ... ')
for device in devices:
try:
setdefaulttimeout(5)
url=f"https://{args.username}:{args.password}@{device}/command-api"
switch = Server(url)
switch.runCmds(1, ['enable'])
except jsonrpc.TransportError:
print('wrong credentials for ' + device)
unreachable.append(device)
except OSError:
print(device + ' is not reachable using eAPI')
unreachable.append(device)
for item in unreachable:
devices.remove(item)
if len(devices) > 0:
# Progress bar
number_of_unreachable_devices = len(unreachable)
number_of_reachable_devices = len(devices)
pbar = tqdm(total = number_of_unreachable_devices + number_of_reachable_devices,\
desc = 'Collecting files from devices')
pbar.update(number_of_unreachable_devices)
# Collect all the tech-support files stored on Arista switches flash and copy them locally
for device in devices:
url = "https://" + args.username + ":" + args.password + "@" + device + "/command-api"
try:
# Create one zip file named all_files.zip on the device with the all the show tech-support files in it
switch = Server(url)
zip_command = 'bash timeout 30 zip /mnt/flash/schedule/all_files.zip /mnt/flash/schedule/tech-support/*'
cmds=[zip_command]
switch.runCmds(1,cmds, 'text')
# Get device hostname
cmds=['show hostname']
result = switch.runCmds(1,cmds, 'json')
hostname = result[0]['hostname']
# Create directories
output_dir = device_directories (hostname, args.output_directory)
# Connect to the device using SSH
ssh = create_ssh_client(device, PORT, args.username, args.password)
# Get the zipped file all_files.zip using SCP and save it locally
my_path = output_dir[1] + '/' + date + '_' + hostname + '.zip'
scp = SCPClient(ssh.get_transport())
scp.get("/mnt/flash/schedule/all_files.zip",local_path = my_path)
scp.close()
# Delete the created zip file on the device
cmds=['bash timeout 30 rm /mnt/flash/schedule/all_files.zip']
switch.runCmds(1,cmds, 'text')
pbar.update(1)
except jsonrpc.AppError:
print("Could not collect show tech files on device " + device)
pbar.update(1)
pbar.close()
print('Done. Files are in the directory ' + output_dir[0])
else:
print("can not connect on any device")
if __name__ == '__main__':
main()