-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_backup.py
68 lines (58 loc) · 2.38 KB
/
switch_backup.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
import os
import json
import logging
from datetime import datetime
from netmiko import ConnectHandler
from secrects import switch_user, switch_password
from email_log import email
#Loading the device json file and adding to a variable
device_json = open (os.path.expanduser('~/scripts/switch.json'), 'r')
device_list = json.load(device_json)
device_json.close()
show_run = 'show run'
date = datetime.now().strftime('%Y%m%d')
#Using this variable to compare the number of switches that have been attempted to backup to the number of done devices.
d = 0 #Setting done count to zero
#Creating the log file
logpath = '~/scripts/logging'
logfile = 'switch_backup{}.log'.format(date)
os.makedirs(os.path.expanduser(logpath), exist_ok=True)
logging.basicConfig(
filename=os.path.join(os.path.expanduser(logpath), logfile),
level=logging.WARNING,
format='%(asctime)s:%(levelname)s:%(message)s'
)
#Opening the switch.json file that has all the switch ip information
for switch in device_list['switch_list']:
#print('Attempting to back up {}'.format(switch['hostname']), end='\r')
device = {
'device_type': switch['os'],
'host': switch['ip_address'],
'username': switch_user,
'password': switch_password,
}
#Trying logging into the switch to do a show run and save that to a text file
try:
with ConnectHandler(**device) as net_connect:
no_pager_command = ''
if switch['os'] == 'brocade_fastiron':
no_pager_command = 'skip-page-display'
elif switch['os'] == 'dell_os10':
no_pager_command = 'terminal length 0'
if no_pager_command:
net_connect.send_command(no_pager_command)
running_config = net_connect.send_command(show_run)
net_connect.disconnect()
except Exception as e:
logging.error('Device {} {} has failed the backup: {}'.format(switch['hostname'], switch['ip_address'], e))
continue
###Create folder and file by hostname###
hostname_dir = '~/scripts/config_switch/'+switch['hostname']+'/'
os.makedirs(os.path.expanduser(hostname_dir), exist_ok=True)
hostname_file = 'running_config_{}.txt'.format(date)
f = open(os.path.join(os.path.expanduser(hostname_dir), hostname_file), 'w+')
f.write(running_config)
f.close()
d+=1
if (d < len(device_list['switch_list'])):
email(logpath,logfile)