-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetConfigBackup.py
More file actions
executable file
·286 lines (243 loc) · 8.18 KB
/
netConfigBackup.py
File metadata and controls
executable file
·286 lines (243 loc) · 8.18 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
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
#!/usr/bin/env python
###Import Block###
from configobj import ConfigObj
from datetime import datetime
from getpass import getpass,getuser
import logging
from os import path,makedirs,listdir,remove,rmdir
import shutil
from socket import gethostbyname,gethostname
import string
from subprocess import *
from sys import exit
from threading import *
from time import sleep,time
def clear_old_dir(expRate):
'''Clear old download dirs'''
def get_immediate_subdirectories(dir):
return [name for name in listdir(dir)
if path.isdir(path.join(dir, name))]
basedir = "/tftpboot/"
dirs = get_immediate_subdirectories(basedir)
for dir in dirs:
dir = basedir + dir
mtime = path.getmtime(dir)
mtime = int(mtime)
today = time()
delta = (today - mtime)
print(dir)
if delta > expRate:
shutil.rmtree(dir)
###Function for Config Import###
def get_config(list,cmd,user_pass,en_pass,ip):
username="admin"
for item in list:
if not path.exists("%s/%s" % (smb_rdir,item)):
#Pre-run that removes SSH host key first
Popen(["ssh-keygen","-R",item],stdin=None,stdout=open('/dev/null', 'w'),stderr=open('/dev/null','w'))
sleep(10)
Popen(["ping","-c","4","-i","1",item],stdin=None,stdout=open('/dev/null','w'),stderr=open('/dev/null','w'))
p=Popen([cmd, item, user_pass, en_pass, username,ip], stdin=None, stdout=PIPE)
sleep(30)
p.stdout.close()
p.wait()
if path.exists("%s/%s" % (basedir,item)):
Popen(["cp", "%s/%s" % (basedir,item), "%s" % smb_rdir]).wait()
Popen(["rm","-f", "%s/%s" % (basedir,item)]).wait()
if path.exists("%s/%s" % (smb_rdir,item)):
logging.info("%s remote transfer complete\n" % item)
print("%s remote transfer complete\n" % item)
else:
logging.error("%s remote transfer failed\n" % item)
bad_devices.append(item)
else:
logging.error("%s local download failed" % item)
bad_devices.append(item)
else:
logging.warning("%s/%s already exists...skipping" % (smb_rdir,item))
def getDevices(model):
try:
FILE = eval(config['devicelist'])
except:
FILE = config['devicelist']
infile = open("%s" % FILE)
result = []
for line in infile:
if string.find(line,'#') == -1:
fields = line.split(':')
if len(fields) >= 2:
name = fields[0].rstrip("\r\n")
type = fields[1].rstrip("\r\n")
if type.lower() == model:
result.append(name)
return result
###End Functions###
rundir = "/usr/local/bin/"
config=ConfigObj("%s/config.ini" % rundir)
smb_config=config['smb']
bad_devices=[]
###Set TFTP IP###
tftpIP="%s" % gethostbyname(gethostname())
###Get YYYYMMDD date and establish local basedir###
now = datetime.now()
date = now.strftime("%Y%m%d")
tftpdir= "/tftpboot/"
basedir = tftpdir + date
###Before Anything else, remove expired config downloads###
try:
clear_old_dir(1209600) ##This set the expiration rate @ 2 weeks
except:
print "Cleanup failed"
###Configure Samba###
try:
smb_user = eval(smb_config['user'])
except:
smb_user = smb_config['user']
try:
smb_dom = smb_config['domain']
except:
print "Setting Domain failed"
exit(1)
try:
smb_password = eval(smb_config['password'])
except:
print "Setting Password Failed"
exit(1)
try:
smb_share = smb_config['share']
except:
print "Setting Share Failed"
exit(1)
try:
smb_mount = smb_config['mount']
except:
print "Setting Mount Point Failed"
exit(1)
try:
smb_rdir = eval(smb_config['remote_dir'])
except:
smb_rdir = smb_config['remote_dir']
###Mount Share###
Popen(["sudo","mount", "-t", "cifs", "-o", "username=%s,domain=%s,password=%s" % (smb_user,smb_dom,smb_password), "%s" % smb_share, "%s" % smb_mount])
###Prompt for device passwords###
pass_config=config['passwords']
try:
netdev_user_pass = eval(pass_config['user'])
except:
netdev_user_pass = pass_config['user']
try:
netdev_en_pass = eval(pass_config['enable'])
except:
netdev_en_pass = pass_config['enable']
try:
voipdev_en_pass = eval(pass_config['voip'])
except:
voipdev_en_pass = pass_config['voip']
try:
san_pass = eval(pass_config['san'])
except:
san_pass = pass_config['san']
###Expect Scripts###
##Cisco##
ios_cmd = rundir + "getIOS.exp"
fos_cmd = rundir + "getFOS.exp"
nxos_cmd = rundir + "getNXOS.exp"
nxos_san_cmd = rundir + "getNXOS-SAN.exp"
iosxe_cmd = rundir + "getIOSXE.exp"
##Force10##
ftos_cmd = rundir + "getFTOS.exp"
sftos_cmd = rundir + "getSFTOS.exp"
##Others##
ent_cmd = rundir + "getEnt.exp"
dell_cmd = rundir + "getDell.exp"
dellv2_cmd = rundir + "getDellv2.exp"
dellv3_cmd = rundir + "getDellv3.exp"
###Device List By OS###
##Cisco
ios = getDevices("ios")
iosxe = getDevices("iosxe")
fos = getDevices("fos")
nxos = getDevices("nxos")
nxos_san = getDevices("ciscosan")
voip = getDevices("voip")
##Force10
sftos = getDevices("sftos")
ftos = getDevices("ftos")
##Enterasys
ent = getDevices("ent")
##Dell
dell = getDevices("dell")
dellv2 = getDevices("dellv2")
dellv3 = getDevices("dellv3")
###Create TFTP Path###
if not path.exists(basedir):
Popen(["mkdir", basedir])
Popen(["sudo", "chown", "nobody", basedir])
###Create Remote Path###
if not path.exists(smb_rdir):
makedirs(smb_rdir)
###Configure Logging###
LOG = "%s/runlog.txt" % smb_rdir
logging.basicConfig(filename=LOG,level=logging.INFO,filemode='w',format="%(asctime)s - %(levelname)s - %(message)s")
###Config Import###
##Cisco##
iosThread=Thread(target=get_config,args=(ios,ios_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
fosThread=Thread(target=get_config,args=(fos,fos_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
nxosThread=Thread(target=get_config,args=(nxos,nxos_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
sanThread=Thread(target=get_config,args=(nxos_san,nxos_san_cmd,san_pass,netdev_en_pass,tftpIP))
voipThread=Thread(target=get_config,args=(voip,ios_cmd,netdev_user_pass,voipdev_en_pass,tftpIP))
iosxeThread=Thread(target=get_config,args=(iosxe,iosxe_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
##Force10##
ftosThread=Thread(target=get_config,args=(ftos,ftos_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
#get_config(sftos,sftos_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
##Others##
entThread=Thread(target=get_config,args=(ent,ent_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
dellThread=Thread(target=get_config,args=(dell,dell_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
dellv2Thread=Thread(target=get_config,args=(dellv2,dellv2_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
dellv3Thread=Thread(target=get_config,args=(dellv3,dellv3_cmd,netdev_user_pass,netdev_en_pass,tftpIP))
iosThread.start()
fosThread.start()
nxosThread.start()
sanThread.start()
voipThread.start()
iosxeThread.start()
ftosThread.start()
entThread.start()
dellThread.start()
dellv2Thread.start()
dellv3Thread.start()
iosThread.join()
fosThread.join()
nxosThread.join()
sanThread.join()
voipThread.join()
iosxeThread.join()
ftosThread.join()
entThread.join()
dellThread.join()
dellv2Thread.join()
dellv3Thread.join()
logging.error('Failed Devices:\n%s' % bad_devices)
#for device in bad_devices:
# if device in ios:
# get_config([device],ios_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in fos:
# get_config([device],fos_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in nxos:
# get_config([device],nxos_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in nxos_san:
# get_config([device],nxos_san_cmd,san_pass,netdev_en_pass,tftpIP)
# elif device in voip:
# get_config([device],ios_cmd,netdev_user_pass,voipdev_en_pass,tftpIP)
# elif device in iosxe:
# get_config([device],iosxe_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in ftos:
# get_config([device],ftos_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in ent:
# get_config([device],ent_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in dell:
# get_config([device],dell_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in dellv2:
# get_config([device],dellv2_cmd,netdev_user_pass,netdev_en_pass,tftpIP)
# elif device in dellv3:
# get_config([device],dellv3_cmd,netdev_user_pass,netdev_en_pass,tftpIP)