-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.py
More file actions
48 lines (42 loc) · 1.55 KB
/
Copy pathscript.py
File metadata and controls
48 lines (42 loc) · 1.55 KB
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
import json
import lib.myfunction as fn
import netmiko
import os
import sys
if len(sys.argv) < 3:
print('Usage: script.py commands.txt devices.json')
exit()
netmiko_exceptions = (netmiko.ssh_exception.NetMikoTimeoutException, netmiko.ssh_exception.NetMikoAuthenticationException)
username, password = fn.get_credentials()
with open(sys.argv[1]) as cmd_file:
commands = cmd_file.readlines()
with open(sys.argv[2]) as dev_file:
devices = json.load(dev_file)
for device in devices:
device['username'] = username
device['password'] = password
try:
print('-' * 70)
print('Connecting to device:', device['ip'])
connection = netmiko.ConnectHandler(**device)
connection.enable()
newdir = connection.base_prompt
try:
os.mkdir(newdir)
except OSError as error:
# FileExistsError is error # 17
if error.errno == 17:
print('Directory', newdir, 'already exists.')
else:
# re-raise the exception if some other error occurred.
raise
for command in commands:
filename = command.rstrip().replace(' ', '_') + '.txt'
filename = os.path.join(newdir, filename)
print('Saving Backup ' + command, end='')
with open(filename, 'w') as out_file:
out_file.write(connection.send_command(command) + '\n')
print('Saved Successfully')
connection.disconnect()
except netmiko_exceptions as error:
print('Failed to ', device['ip'], error)