auto diff updater
This commit is contained in:
parent
d8ed3fe873
commit
d506305bf2
2 changed files with 68 additions and 14 deletions
|
@ -307,8 +307,8 @@ if __name__ == "__main__":
|
||||||
python3 frankenrouter.py init --- setup the default firewall
|
python3 frankenrouter.py init --- setup the default firewall
|
||||||
python3 frankenrouter.py allipsetup --- read the contents of /root/pubip.cache and setup all assigments. for startup.
|
python3 frankenrouter.py allipsetup --- read the contents of /root/pubip.cache and setup all assigments. for startup.
|
||||||
|
|
||||||
python3 frankenrouter.py ipadd IP MASK VLAN --- add IP to VLAN
|
python3 frankenrouter.py ipadd IP VLAN --- add IP to VLAN
|
||||||
example: ipadd 87.120.110.120 24 142
|
example: ipadd 87.120.110.120 142
|
||||||
|
|
||||||
python3 frankenrouter,py ipdel IP VLAN --- del IP from VLAN
|
python3 frankenrouter,py ipdel IP VLAN --- del IP from VLAN
|
||||||
example: ipdel 87.120.110.120 142
|
example: ipdel 87.120.110.120 142
|
||||||
|
@ -328,7 +328,7 @@ python3 frankenrouter,py ipdel IP VLAN --- del IP from VLAN
|
||||||
allipsetup('/root/pubip.cache', ip_mask)
|
allipsetup('/root/pubip.cache', ip_mask)
|
||||||
|
|
||||||
if sys.argv[1] == 'ipadd':
|
if sys.argv[1] == 'ipadd':
|
||||||
bashexec('ipadd-{}-{}-{}'.format(sys.argv[2], ip_mask, sys.argv[3]), assignip(sys.argv[2], ip_mask, sys.argv[3]))
|
bashexec('ipadd-{}-{}'.format(sys.argv[2], sys.argv[3]), assignip(sys.argv[2], ip_mask, sys.argv[3]))
|
||||||
|
|
||||||
if sys.argv[1] == 'ipdel':
|
if sys.argv[1] == 'ipdel':
|
||||||
bashexec('ipdel-{}-{}'.format(sys.argv[2], sys.argv[3]), removeip(sys.argv[2], sys.argv[3]))
|
bashexec('ipdel-{}-{}'.format(sys.argv[2], sys.argv[3]), removeip(sys.argv[2], sys.argv[3]))
|
||||||
|
|
|
@ -4,12 +4,28 @@ import requests
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
conffile = open('/root/frankenrouter/config.sh', 'r')
|
class DictDiffer(object):
|
||||||
for line in conffile:
|
"""
|
||||||
if re.search('LABEL', line):
|
Calculate the difference between two dictionaries as:
|
||||||
slave_name = line.split('=', 1)[1].rstrip().replace('"', '')
|
(1) items added
|
||||||
conffile.close()
|
(2) items removed
|
||||||
|
(3) keys same in both but changed values
|
||||||
|
(4) keys same in both and unchanged values
|
||||||
|
"""
|
||||||
|
def __init__(self, current_dict, past_dict):
|
||||||
|
self.current_dict, self.past_dict = current_dict, past_dict
|
||||||
|
self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
|
||||||
|
self.intersect = self.set_current.intersection(self.set_past)
|
||||||
|
def added(self):
|
||||||
|
return self.set_current - self.intersect
|
||||||
|
def removed(self):
|
||||||
|
return self.set_past - self.intersect
|
||||||
|
def changed(self):
|
||||||
|
return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
|
||||||
|
def unchanged(self):
|
||||||
|
return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
|
||||||
|
|
||||||
conffile = open('/root/frankenrouter/config.sh', 'r')
|
conffile = open('/root/frankenrouter/config.sh', 'r')
|
||||||
for line in conffile:
|
for line in conffile:
|
||||||
|
@ -19,6 +35,12 @@ conffile.close()
|
||||||
|
|
||||||
api_url = 'https://' + str(api_host) + '/vmanager/slavetables'
|
api_url = 'https://' + str(api_host) + '/vmanager/slavetables'
|
||||||
|
|
||||||
|
conffile = open('/root/frankenrouter/config.sh', 'r')
|
||||||
|
for line in conffile:
|
||||||
|
if re.search('LABEL', line):
|
||||||
|
slave_name = line.split('=', 1)[1].rstrip().replace('"', '')
|
||||||
|
conffile.close()
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -26,15 +48,47 @@ try:
|
||||||
apireq = requests.post(api_url, headers={'Content-Type': 'application/json'}, data=json.dumps(data), timeout=30)
|
apireq = requests.post(api_url, headers={'Content-Type': 'application/json'}, data=json.dumps(data), timeout=30)
|
||||||
result = apireq.json()
|
result = apireq.json()
|
||||||
except:
|
except:
|
||||||
|
print('can not connect')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
if result['status'] == 'ok':
|
if result['status'] == 'ok':
|
||||||
del result['status']
|
del result['status']
|
||||||
wr = open('/root/pubip.cache', 'w')
|
new_list = result
|
||||||
#TODO: Sort cache and compare.
|
|
||||||
wr.write(json.dumps(result))
|
# read cache to load the old values
|
||||||
wr.close()
|
r_ca = open('/root/pubip.cache', 'r')
|
||||||
print('public ip cache updated')
|
current_list = json.loads(r_ca.read())
|
||||||
|
r_ca.close()
|
||||||
|
|
||||||
|
#compare the current value with the cache
|
||||||
|
newdataflag = False
|
||||||
|
difference = DictDiffer(new_list, current_list)
|
||||||
|
if len(difference.added()) is not 0:
|
||||||
|
for ipkey in difference.added():
|
||||||
|
ip = ipkey
|
||||||
|
vlan = new_list[ipkey]
|
||||||
|
print('added {} to {}'.format(ip, vlan))
|
||||||
|
newdataflag = True
|
||||||
|
subprocess.call('python3 /root/frankenrouter/frankenrouter.py ipadd {} {}'.format(ip, vlan), shell=True)
|
||||||
|
if len(difference.removed()) is not 0:
|
||||||
|
for ipkey in difference.removed():
|
||||||
|
ip = ipkey
|
||||||
|
vlan = current_list[ipkey]
|
||||||
|
print('removed {} from {}'.format(ip, vlan))
|
||||||
|
newdataflag = True
|
||||||
|
subprocess.call('python3 /root/frankenrouter/frankenrouter.py ipdel {} {}'.format(ip, vlan), shell=True)
|
||||||
|
|
||||||
|
if newdataflag:
|
||||||
|
# move the old cache and write the new data
|
||||||
|
subprocess.call('mv /root/pubip.cache /root/pubip.cache.old', shell=True)
|
||||||
|
w_ca = open('/root/pubip.cache', 'w')
|
||||||
|
w_ca.write(json.dumps(new_list))
|
||||||
|
w_ca.close()
|
||||||
|
print('public ip cache updated with the new data')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('no data')
|
print('no data error')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue