-
Notifications
You must be signed in to change notification settings - Fork 7
/
autoback.py
80 lines (66 loc) · 2.05 KB
/
autoback.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
#!/usr/bin/env python
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import xml.etree.ElementTree as ET
import sys
sys.path.insert(0, '/var/dug/')
import fw_creds
fwhost = fw_creds.fwhost
fwkey = fw_creds.fwkey
import datetime
now = datetime.datetime.now()
stamp = "%s-%s-%s-%s-%s-%s" % (now.year, now.month, now.day, now.hour, now.minute, now.second)
import os.path
def lastBack():
if (os.path.isfile("/var/autoback/lastback.txt")):
file = open("/var/autoback/lastback.txt", "r")
lastpcap = file.read()
file.close()
return lastpcap
else:
return False
def getLastBack(lastback):
try:
file = open(lastback, "r")
config = file.read()
return config
except:
return False
def getConfig(fwhost, fwkey):
values = {'type': 'op', 'cmd': '<show><config><running/></config></show>', 'key': fwkey}
call = 'https://' + fwhost + '/api'
try:
request = requests.post(call, data=values, verify=False)
tree = ET.fromstring(request.text)
config = tree.find('./result/config')
return ET.tostring(config)
except:
return False
lastback = lastBack()
if lastback:
previousconfig = getLastBack(lastback)
currentconfig = getConfig(fwhost, fwkey)
if(previousconfig and currentconfig):
if previousconfig != currentconfig:
filename = "/var/autoback/%s.xml" % (stamp, )
file = open(filename, "w")
file.write(currentconfig)
file.close()
file = open("/var/autoback/lastback.txt", "w")
file.write(filename)
file.close()
else:
if not previousconfig:
print "There was a problem reading the latest backup file"
if not currentconfig:
print "There was a problem getting the current configuration from the firewall"
else:
runningconfig = getConfig(fwhost, fwkey)
filename = "/var/autoback/%s.xml" % (stamp, )
file = open(filename, "w")
file.write(runningconfig)
file.close()
file = open("/var/autoback/lastback.txt", "w")
file.write(filename)
file.close()