-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswitchbackup.py
192 lines (170 loc) · 6.88 KB
/
switchbackup.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
import concurrent.futures
import datetime
import logging
from multiprocessing import cpu_count
import os
import pathlib
import pexpect
import sys
import time
from ruamel.yaml import YAML
# h3c 交换机配置备份
def H3CAutoConfig(host):
ssh = pexpect.spawn('ssh -o StrictHostKeyChecking=no -p %s %s@%s ' %
(host['port'], host['username'], host['ip']))
try:
ssh.expect('[Pp]assword:', timeout=30)
ssh.sendline(host['password'])
ssh_info = ssh.expect(['>$', '[Pp]assword:'])
if ssh_info == 0:
ssh.sendline('screen-length disable')
ssh.expect(['>$', ']$'])
ssh.sendline('dis cur')
ssh.expect(['>$', ']$'])
with open(log_filename + '/' + host['ip'] + '.txt', 'w') as f:
print(ssh.before.decode('utf8', 'ignore').replace('\r', ''),
file=f)
ssh.close()
logger.info('backup switch {}[{}:{}] successful \n'.format(
host['name'], host['ip'], host['port']))
else:
ssh.close()
logger.error('switch {}[{}:{}] password error \n'.format(
host['name'], host['ip'], host['port']))
except pexpect.EOF:
ssh.close()
logger.error('switch {}[{}:{}] ssh failed.(EOF) \n'.format(
host['name'], host['ip'], host['port']))
except pexpect.TIMEOUT:
ssh.close()
logger.error('switch {}[{}:{}] ssh failed.(TIMEOUT) \n'.format(
host['name'], host['ip'], host['port']))
# 华为 交换机配置备份
def HuaweiAutoConfig(host):
ssh = pexpect.spawn('ssh -o StrictHostKeyChecking=no -p %s %s@%s ' %
(host['port'], host['username'], host['ip']))
try:
ssh.expect('[Pp]assword:', timeout=30)
ssh.sendline(host['password'])
ssh_info = ssh.expect(['>$', '[Pp]assword:'])
if ssh_info == 0:
ssh.sendline('screen-length 0 temporary')
ssh.expect(['>$', ']$'])
ssh.sendline('dis cur')
ssh.expect(['>$', ']$'])
with open(log_filename + '/' + host['ip'] + '.txt', 'w') as f:
print(ssh.before.decode('utf8', 'ignore').replace('\r', ''),
file=f)
ssh.close()
logger.info('backup switch {}[{}:{}] successful \n'.format(
host['name'], host['ip'], host['port']))
else:
ssh.close()
logger.error('switch {}[{}:{}] password error \n'.format(
host['name'], host['ip'], host['port']))
except pexpect.EOF:
ssh.close()
logger.error('switch {}[{}:{}] ssh failed.(EOF) \n'.format(
host['name'], host['ip'], host['port']))
except pexpect.TIMEOUT:
ssh.close()
logger.error('switch {}[{}:{}] ssh failed.(TIMEOUT) \n'.format(
host['name'], host['ip'], host['port']))
# 锐捷/思科 交换机配置备份
def RuijieAutoConfig(host):
ssh = pexpect.spawn('ssh -o StrictHostKeyChecking=no -p %s %s@%s ' %
(host['port'], host['username'], host['ip']))
try:
ssh.expect('[Pp]assword: ', timeout=30)
ssh.sendline(host['password'])
ssh_info = ssh.expect(['#$', '[Pp]assword:'])
if ssh_info == 0:
ssh.sendline('terminal length 0')
ssh.expect('#$')
ssh.sendline('sh run')
ssh.expect('#$')
with open(log_filename + '/' + host['ip'] + '.txt', 'w') as f:
print(ssh.before.decode('utf8', 'ignore').replace('\r', ''),
file=f)
ssh.close()
logger.info('backup switch {}[{}:{}] successful \n'.format(
host['name'], host['ip'], host['port']))
else:
ssh.close()
logger.error('switch {}[{}:{}] password error \n'.format(
host['name'], host['ip'], host['port']))
except pexpect.EOF:
ssh.close()
logger.error('switch {}[{}:{}] ssh failed.(EOF) \n'.format(
host['name'], host['ip'], host['port']))
except pexpect.TIMEOUT:
ssh.close()
logger.error('switch {}[{}:{}] ssh failed.(TIMEOUT) \n'.format(
host['name'], host['ip'], host['port']))
# 检测主机端口是否连通
def host_alive(host):
i = os.system('nc -zv {} {} -w 5'.format(host['ip'], host['port']))
if i != 0:
logger.error('switch {}[{}:{}] is unreachable \n'.format(
host['name'], host['ip'], host['port']))
return False
return True
def backup(host):
if host['type'] == 'h3c':
H3CAutoConfig(host)
elif host['type'] == 'huawei':
HuaweiAutoConfig(host)
elif host['type'] == 'ruijie':
RuijieAutoConfig(host)
elif host['type'] == 'cisco':
RuijieAutoConfig(host)
else:
logger.error('switch {}[{}:{}] is not support \n'.format(
host['name'], host['ip'], host['port']))
# load config file
config_file = pathlib.Path(r'./hosts.yaml')
yaml = YAML(typ='safe')
config = yaml.load(config_file)
date_date = datetime.datetime.now().strftime('%Y-%m-%d')
log_filename = config['backup_path'] + os.sep + date_date
os.system('mkdir -p %s' % log_filename)
# init logger
logger = logging.getLogger()
logger.setLevel('DEBUG')
BASIC_FORMAT = "%(asctime)s:%(levelname)s:%(message)s"
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(BASIC_FORMAT, DATE_FORMAT)
chlr = logging.StreamHandler() # 输出到控制台的handler
chlr.setFormatter(formatter)
chlr.setLevel('DEBUG') # 也可以不设置,不设置就默认用logger的level
fhlr = logging.FileHandler(filename=log_filename + '/error-' + str(date_date) +
'.log',
mode='a+',
encoding='utf8') # 输出到文件的handler
fhlr.setFormatter(formatter)
logger.addHandler(chlr)
logger.addHandler(fhlr)
cpus = cpu_count()
alive_host = []
if len(sys.argv) > 1:
for host in config['hosts']:
if host['ip'] in sys.argv[1:] and host_alive(host):
alive_host.append(host)
else:
for host in config['hosts']:
if host_alive(host):
alive_host.append(host)
with concurrent.futures.ThreadPoolExecutor(max_workers=cpus) as executor:
submits = [executor.submit(backup, host) for host in alive_host]
for future in concurrent.futures.as_completed(submits):
future.result()
nowtime = datetime.datetime.now()
# 删除过期的配置文件
backup_filelist = list(os.listdir(config['backup_path']))
for backup_file in backup_filelist:
filetime = datetime.datetime.fromtimestamp(
os.path.getmtime(config['backup_path'] + os.sep + backup_file))
if (nowtime - filetime).seconds > config['keep_time'] * 24 * 3600:
logger.info('delete backup files before {} days[{}]'.format(
config['keep_time'], filetime))
os.system('rm -rf ' + config['backup_path'] + os.sep + backup_file)