-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnjsupport.py
328 lines (301 loc) · 14.4 KB
/
njsupport.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# Developed by Christian Scholz [Mail: [email protected], Mastodon: @[email protected], Twitter: @chsjuniper]
# Feel free to use and modify this script as needed but use at your own risk!
# This is a hobby project - I'm not employed by Juniper.
# I just wanted a tool that helps me doing my every day work a bit faster and more consistent
# You've been warned ;)
import os
import sys
import time
import shutil
import getpass
import logging
import paramiko
from pathlib import Path
from scp import SCPClient
from jnpr.junos import Device
from datetime import datetime
from jnpr.junos.utils.start_shell import StartShell
varIP = input("Please enter the hostname or IP of your target device: ")
varUser = input("Please enter a username (not root): ")
varPassword = getpass.getpass()
version_arg = "2023.09.21.9773"
now = datetime.now()
dir_config = 'configuration'
dir_rsi = 'rsi'
dir_core = 'core-dumps'
dir_logfiles = 'logfiles'
dir_root = 'upload'
date_arg = now.strftime("%Y-%m-%d_%H-%M-%S")
# pyinstaller -F -i Icon.ico -n Output-Filename script-source.py
# Set up logging
log = "njs_" + date_arg + ".log"
logging.basicConfig(filename=log, level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
if str(varUser) == 'root':
sys.exit(
'Which part of NOT root did you not understand? - Please run the tool again and choose another user.')
while True:
try:
varPath = str(input("Do you want the support package [R]emote (on the box) or [L]ocal (download to this PC)?: "))
except ValueError:
print("Please choose either R for Remote or L for Local")
else:
if varPath=="R" or varPath=="L":
break
else:
print("Please choose either R for Remote or L for Local")
buff = ''
resp = ''
print("\n")
print("\n")
print("\n")
print("###############################################################################")
print("# Version:", version_arg, " #")
print("# This script will run all necessary troubleshooting commands for you. #")
print("# #")
print("# WARNING: Please leave this window open and running. #")
print("# #")
print("# After the program is finished, it will automatically close itself. #")
print("# #")
print("###############################################################################")
print("\n")
print("\n")
print("\n")
print("Script is starting...")
logging.info('Script is starting...')
time.sleep(2)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(varIP, username=varUser, password=varPassword)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
channel = ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command('show virtual-chassis status | match fpc', get_pty=True)
vcmember_list = []
for line in iter(stdout.readline, ""):
vcmember_list.append(line[0])
# Creating necessary folders (if they don't exist) so root folder does not get messy
logging.info('Trying to create necessary folders')
if not os.path.exists(dir_root + '-' + varIP + '-' + date_arg):
os.mkdir(dir_root + '-' + varIP + '-' + date_arg)
logging.info('Info: root directory created successfully')
else:
logging.info('Info: root directory already existed!')
if not os.path.exists(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_config):
os.mkdir(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_config)
logging.info('Info: directory for configuration-files created successfully')
else:
logging.info('Info: directory for configuration-files already existed!')
if not os.path.exists(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_rsi):
os.mkdir(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_rsi)
logging.info('Info: directory for rsi-files created successfully')
else:
logging.info('Info: directory for rsi-files already existed!')
if not os.path.exists(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_core):
os.mkdir(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_core)
logging.info('Info: directory for core-dumps created successfully')
else:
logging.info('Info: directory for core-dumps already existed!')
if not os.path.exists(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_logfiles):
os.mkdir(dir_root + '-' + varIP + '-' + date_arg + '/' + dir_logfiles)
logging.info('Info: directory for logfiles created successfully')
else:
logging.info('Info: directory for logfiles already existed!')
# Saving the config
print("\n")
print("Step 1/5: Saving the active configuration in set-format (including secrets)")
logging.info('Step1/5: Saving the active configuration in set-format (including secrets)')
stdin, stdout, stderr = ssh.exec_command(
'show configuration | display set | no-more | save /var/tmp/active-config-' + date_arg + '.txt\n')
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
logging.info('Info: Configuration saved successfully.')
else:
logging.info('Error: Could not save configuration.')
# Creating the RSI (save to file) and wait for it to complete
# See https://www.juniper.net/documentation/en_US/junos/topics/reference/command-summary/request-support-information.html
print("\n")
print("Step 2/5: Creating the RSI")
logging.info('Step 2/5: Creating the RSI')
if len(vcmember_list) == 1:
stdin, stdout, stderr = ssh.exec_command('request support information | save /var/tmp/rsi-' + date_arg + '.txt\n')
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
logging.info('Info: RSI created successfully.')
else:
logging.info('Error: Could not create RSI. Please check the device manually.')
else:
stdin, stdout, stderr = ssh.exec_command('request support information all-members | save /var/tmp/rsi_vc-' + date_arg + '.txt\n')
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
logging.info('Info: RSI created successfully.')
else:
logging.info('Error: Could not create RSI. Please check the device manually.')
ssh.close()
# Compressing the Logfiles
# See https://www.juniper.net/documentation/en_US/junos/topics/task/troubleshooting/troubleshooting-logs-compressing.html
print("\n")
print("Step 3/5: Compressing the Logfiles")
logging.info('Step 3/5: Compressing the Logfiles')
if len(vcmember_list) == 1:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(varIP, username=varUser, password=varPassword)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
channel = ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command(
'file archive compress source /var/log/* destination /var/tmp/logfiles-' + date_arg + '.tgz\n')
exit_status = stdout.channel.recv_exit_status()
if exit_status == 0:
logging.info('Info: Logfiles compressed successfully.')
ssh.close()
else:
logging.info('Error: Logfiles not compressed successfully. Check Device manually.')
ssh.close()
else:
for i in vcmember_list:
dev_ntest = Device(host=varIP, user=varUser, password=varPassword,normalize=True)
try:
dev_ntest.open()
with StartShell(dev_ntest) as bsd:
bsd.run('rlogin -Ji fpc' + str(i), timeout=1)
bsd.wait_for(this='Password:', timeout=1)
bsd.send(varPassword)
bsd.run('start shell', timeout=1)
bsd.wait_for(this='%', timeout=1)
bsd.run('tar -zcvf /var/tmp/varlog-mem' + str(i) + '.tar.gz /var/log/* ')
bsd.wait_for(this='%', timeout=1)
bsd.run('cli -c "file copy /var/tmp/varlog-mem' + str(i) + '.tar.gz fpc'+ str(varVCID_master) +':/var/tmp/"')
bsd.wait_for(this='%', timeout=1)
dev_ntest.close()
except:
logging.info('Error: VC-Logfiles not compressed successfully. Check Device manually.')
dev_ntest.close()
# Make single archive for VC
dev_ntest = Device(host=varIP, user=varUser, password=varPassword,normalize=True)
dev_ntest.open()
with StartShell(dev_ntest) as bsd:
bsd.run('tar -zcvf /var/tmp/varlog-all-vc-members-' + date_arg + '.tar.gz /var/tmp/varlog-mem* ', timeout=1)
bsd.wait_for(this='%', timeout=1)
dev_ntest.close()
if varPath == "L":
# Now downloading all the files created on the device via scp
print("\n")
print("Step 4/5: Fetching the files created earlier")
logging.info('Step 4/5: Fetching the files created earlier')
logging.info('Info: Fetching RSI...')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(varIP, username=varUser, password=varPassword)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
channel = ssh.invoke_shell()
if len(vcmember_list) == 1:
try:
with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
scp.get(remote_path='/var/tmp/rsi-' + date_arg + '.txt',
local_path='./' + dir_root + '-' + varIP + '-' + date_arg + '/' + dir_rsi + '/')
except:
logging.info('Error: Could not fetch RSI - something went wrong...')
scp.close()
finally:
logging.info('Info: RSI successfully fetched.')
scp.close()
else:
try:
with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
scp.get(remote_path='/var/tmp/rsi_vc-' + date_arg + '.txt',
local_path='./' + dir_root + '-' + varIP + '-' + date_arg + '/' + dir_rsi + '/')
except:
logging.info('Error: Could not fetch RSI - something went wrong...')
scp.close()
finally:
logging.info('Info: RSI successfully fetched.')
scp.close()
logging.info('Info: Fetching Logfiles...')
if len(vcmember_list) == 1:
try:
with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
scp.get(remote_path='/var/tmp/logfiles-' + date_arg + '.tgz',
local_path='./' + dir_root + '-' + varIP + '-' + date_arg + '/' + dir_logfiles + '/')
except:
logging.info('Error: Could not fetch Logfiles - something went wrong...')
scp.close()
finally:
logging.info('Info: Logfiles successfully fetched.')
scp.close()
else:
try:
with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
scp.get(remote_path='/var/tmp/varlog-all-vc-members-' + date_arg + '.tar.gz',
local_path='./' + dir_root + '-' + varIP + '-' + date_arg + '/' + dir_logfiles + '/')
except:
logging.info('Error: Could not fetch Logfiles - something went wrong...')
scp.close()
finally:
logging.info('Info: Logfiles successfully fetched.')
scp.close()
logging.info('Info: Fetching Configuration...')
try:
with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
scp.get(remote_path='/var/tmp/active-config-' + date_arg + '.txt',
local_path='./' + dir_root + '-' + varIP + '-' + date_arg + '/' + dir_config + '/')
except:
logging.info('Error: Could not fetch active Configuration - something went wrong...')
scp.close()
finally:
logging.info('Info: Configuration successfully fetched.')
scp.close()
logging.info('Info: Now fetching the crash-dumps (core-dumps) if they exist...')
try:
with SCPClient(ssh.get_transport(), sanitize=lambda x: x) as scp:
scp.get(remote_path='/var/crash/*',
local_path='./' + dir_root + '-' + varIP + '-' + date_arg + '/' + dir_core + '/')
except:
logging.info('Info: No crash-dumps (core-dumps) found - this is a good sign.')
scp.close()
finally:
logging.info('Warning: crash-dumps (core-dumps) found and transferred...')
scp.close()
print("\n")
print("Step 5/5: Deleting files from remote device to gain space back and finishing script")
logging.info('Step 5/5: Deleting files from remote device to gain space back and finishing script')
logging.info('Info: Deleting /var/tmp/rsi-' + date_arg + '.txt')
channel.send('file delete /var/tmp/rsi-' + date_arg + '.txt\n')
logging.info('Info: File deleted successfully.')
time.sleep(2)
logging.info('Info: Deleting /var/tmp/rsi_vc-' + date_arg + '.txt')
channel.send('file delete /var/tmp/rsi_vc-' + date_arg + '.txt\n')
logging.info('Info: File deleted successfully.')
time.sleep(2)
logging.info('Info: Deleting /var/tmp/logfiles-' + date_arg + '.tgz')
channel.send('file delete /var/tmp/logfiles-' + date_arg + '.tgz\n')
logging.info('Info: File deleted successfully.')
time.sleep(2)
logging.info('Info: Deleting /var/tmp/varlog-*')
channel.send('file delete /var/tmp/varlog-*\n')
logging.info('Info: File deleted successfully.')
time.sleep(2)
logging.info('Info: Deleting /var/tmp/active-config-' + date_arg + '.txt')
channel.send('file delete /var/tmp/active-config-' + date_arg + '.txt\n')
logging.info('Info: File deleted successfully.')
resp = channel.recv(9999)
output = resp.decode().split(',')
time.sleep(1)
ssh.close()
time.sleep(1)
shutil.make_archive('njs-package_' + varIP + '_' + date_arg, 'zip', dir_root + '-' + varIP + '-' + date_arg)
shutil.rmtree(dir_root + '-' + varIP + '-' + date_arg, ignore_errors=True)
print("\n")
print("Finished!")
pathdisplay = Path(__file__).parent.absolute()
print("The file has been downloaded to: " + str(pathdisplay))
elif varPath == "R":
print("\n")
print("Step 4/5 and 5/5: Skipping this steps because you selected that the file should remain on the box.")
logging.info('Step 4/5 and 5/5: Skipping this steps because you selected that the file should remain on the box.')
print("The package can be found at /var/tmp/")
logging.info('The package can be found at /var/tmp/')
print("\n")
print("Finished!")
else:
print("\n")
print("Error: Something went horribly wrong for reasons we do not know yet. Exiting...")
logging.info('Error: Something went horribly wrong for reasons we do not know yet. Exiting...')