-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnmk_send_conf_command.py
158 lines (134 loc) · 5.79 KB
/
nmk_send_conf_command.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
"""
Build list of all devices using cdp crawling.
Send commands from _cisco_commands.txt file to devices defined in PATTERNS.
By default save running-config to memory.
Config saving can be overridden by adding variable write_memory=False to nmk_send_conf_command function
Backup config as hostname-YYYY-MM-DD text file to the path defined in file _backup_path.txt.
If there is no the file or it is empty config files will be written in working directory.
"""
from datetime import datetime
from pathlib import Path
import yaml
import sys
from devactions import Device, credentials_input, nmk_send_conf_command, nmk_send_command
from get_cdp_neighbors import get_hosts_cdp_neighbors
# patterns of device models (platforms) which commands should be sent to
config_file = 'nmk_send_conf_command_config.yml'
# connection_params = {
# 'device_type': 'cisco_ios',
# 'host': '',
# 'username': '',
# 'password': '',
# 'secret': '',
# 'verbose': 'True'
# }
try:
# Open file with config.
with open(config_file, 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
except:
print(f'No configuration file, "{config_file}"')
sys.exit()
try:
backup_path = Path(cfg['parameters']['paths_and_files']['backup_path'])
except:
print(f'No backup path has been found in confugutation file {config_file}')
print('parameters:')
print(' paths_and_files:')
print(' backup_path: "SOME\PATH\HERE"\n')
sys.exit()
try:
file_with_commands = Path(cfg['parameters']['paths_and_files']['file_with_commands'])
except:
print(f'No files with commands has been found in confugutation file {config_file}')
print('parameters:')
print(' paths_and_files:')
print(' file_with_commands: "SOME\PATH\\file_with_commands.txt"\n')
sys.exit()
try:
device_patterns = set(cfg['parameters']['device_patterns'].split(','))
except:
print(f'No device patterns has been found in confugutation file {config_file}')
print('parameters:')
print(' device_patterns: some,patterns,here\n')
sys.exit()
try:
connection_params = cfg['parameters']['connection_params']
except:
print(f'No connection parameters for netmiko has been found in confugutation file {config_file}')
print('parameters:')
print(""" connection_params: {
'device_type': 'netmiko_device_type',
'host': '',
'username': '',
'password': '',
'secret': '',
'verbose': 'True'
}\n""")
sys.exit()
try:
write_memory = cfg['parameters']['write_memory']
except:
print(f'No "write_memory" parameter has been found in confugutation file {config_file}')
print(' "write_memory" is set to True')
write_memory = True
try:
# Open file with commands.
with open(file_with_commands, "r") as f:
commands = f.read().splitlines()
except:
print('No file _cisco_commands.txt with commands')
sys.exit()
# Getting today date
now = datetime.now()
year = f'{now.year}'
month = f'{now.month:02d}'
day = f'{now.day:02d}'
# Asking user for ip to start cdp crawlig from and user credentials for accessing devices
start_ip = input('Enter device ip to start from: ')
username, password, optional_args = credentials_input()
connection_params['username'] = username
connection_params['password'] = password
connection_params['secret'] = optional_args['secret']
# Building list of devices by using cdp crawling function
hosts_cdp = get_hosts_cdp_neighbors(start_ip, username, password, optional_args)
processed_hosts = []
for host in hosts_cdp:
if device_patterns:
# If PATTERNS exist send command only to devices which match the PATTERNS
for pattern in device_patterns:
if pattern in host.platform or pattern in host.name or pattern == 'any':
connection_params['host'] = host.ip
print(f'Connecting to host {host.name} {host.ip}\n')
# Sending commands to device
output = nmk_send_conf_command(connection_params, commands,write_memory = write_memory)
print(output)
# Getting hostname from device using NAPALM
with Device(host.ip, username, password, optional_args=optional_args) as device:
hostname = device.facts['hostname']
config = device.running_config
filename = f'{hostname}-{year}-{month}-{day}' # Compile filename
full_filename = Path(backup_path, filename) # Compile filename
with open(full_filename, 'w') as backup:
backup.write(config)
print(f'Backup of {hostname} completed successfully\n')
processed_hosts.append(hostname)
print(f'Processed hosts: {len(processed_hosts)}\n')
for host in processed_hosts:
print(host)
# else:
# # If PATTERNS don't exist send commands to each device from the list
# connection_params['host'] = host.ip
# # Sending commands to device
# output = nmk_send_conf_command(connection_params, commands)
# print(output)
# # Getting hostname from device using NAPALM
# with Device(host.ip, username, password, optional_args=optional_args) as device:
# hostname = device.facts['hostname']
# filename = f'{backup_path}{hostname}-{year}-{month}-{day}' # Compile filename
# command = 'sh run'
# result = nmk_send_command(connection_params, command=command)
# # Writing config to the file
# with open(filename, 'w') as backup:
# backup.write(result)
# print(f'Backup of {hostname} completed successfully\n')