From 7a27726e0e1e1de47f8abfb64e9c28eadb34c55b Mon Sep 17 00:00:00 2001 From: Dmytro Aleksandrov Date: Sat, 18 Aug 2018 00:43:12 +0300 Subject: T784: Added update dns dynamic operation --- Makefile | 1 - op-mode-definitions/dns-forwarding.xml | 3 ++ op-mode-definitions/dynamic-dns.xml | 25 ++++++++- src/op_mode/dynamic_dns.py | 92 ++++++++++++++++++++++++++++++++++ src/op_mode/dynamic_dns_status.py | 67 ------------------------- 5 files changed, 119 insertions(+), 69 deletions(-) create mode 100755 src/op_mode/dynamic_dns.py delete mode 100755 src/op_mode/dynamic_dns_status.py diff --git a/Makefile b/Makefile index 823637e84..70101181d 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,6 @@ op_mode_definitions: # XXX: delete top level op mode node.def's that now live in other packages rm -f $(OP_TMPL_DIR)/set/node.def rm -f $(OP_TMPL_DIR)/show/node.def - rm -f $(OP_TMPL_DIR)/show/dns/node.def rm -f $(OP_TMPL_DIR)/reset/node.def rm -f $(OP_TMPL_DIR)/restart/node.def rm -f $(OP_TMPL_DIR)/monitor/node.def diff --git a/op-mode-definitions/dns-forwarding.xml b/op-mode-definitions/dns-forwarding.xml index e789f4aee..be71302cd 100644 --- a/op-mode-definitions/dns-forwarding.xml +++ b/op-mode-definitions/dns-forwarding.xml @@ -4,6 +4,9 @@ + + Show DNS information + diff --git a/op-mode-definitions/dynamic-dns.xml b/op-mode-definitions/dynamic-dns.xml index c67769a83..76c473fd7 100644 --- a/op-mode-definitions/dynamic-dns.xml +++ b/op-mode-definitions/dynamic-dns.xml @@ -4,6 +4,9 @@ + + Show DNS information + @@ -14,7 +17,7 @@ Show Dynamic DNS status - sudo ${vyos_op_scripts_dir}/dynamic_dns_status.py + sudo ${vyos_op_scripts_dir}/dynamic_dns.py --status @@ -22,4 +25,24 @@ + + + Update data for a service + + + + + Update DNS information + + + + + Update Dynamic DNS information + + sudo ${vyos_op_scripts_dir}/dynamic_dns.py --update + + + + + diff --git a/src/op_mode/dynamic_dns.py b/src/op_mode/dynamic_dns.py new file mode 100755 index 000000000..7ac3dfe9f --- /dev/null +++ b/src/op_mode/dynamic_dns.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +import os +import argparse +import jinja2 +import sys +import time + +from vyos.config import Config + +cache_file = r'/var/cache/ddclient/ddclient.cache' + +OUT_TMPL_SRC = """ +{%- for entry in hosts -%} +ip address : {{ entry.ip }} +host-name : {{ entry.host }} +last update : {{ entry.time }} +update-status: {{ entry.status }} + +{% endfor -%} +""" + + +def show_status(): + # Do nothing if service is not configured + c = Config() + if not c.exists_effective('service dns dynamic'): + print("Dynamic DNS not configured") + sys.exit(0) + + data = { + 'hosts': [] + } + + with open(cache_file, 'r') as f: + for line in f: + if line.startswith('#'): + continue + + outp = { + 'host': '', + 'ip': '', + 'time': '' + } + + if 'host=' in line: + host = line.split('host=')[1] + if host: + outp['host'] = host.split(',')[0] + + if 'ip=' in line: + ip = line.split('ip=')[1] + if ip: + outp['ip'] = ip.split(',')[0] + + if 'atime=' in line: + atime = line.split('atime=')[1] + if atime: + tmp = atime.split(',')[0] + outp['time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(tmp, base=10))) + + if 'status=' in line: + status = line.split('status=')[1] + if status: + outp['status'] = status.split(',')[0] + + data['hosts'].append(outp) + + tmpl = jinja2.Template(OUT_TMPL_SRC) + print(tmpl.render(data)) + + +def update_ddns(): + os.system('systemctl stop ddclient') + os.remove(cache_file) + os.system('systemctl start ddclient') + + +def main(): + parser = argparse.ArgumentParser() + group = parser.add_mutually_exclusive_group() + group.add_argument("--status", help="Show DDNS status", action="store_true") + group.add_argument("--update", help="Update DDNS on a given interface", action="store_true") + args = parser.parse_args() + + if args.status: + show_status() + elif args.update: + update_ddns() + + +if __name__ == '__main__': + main() diff --git a/src/op_mode/dynamic_dns_status.py b/src/op_mode/dynamic_dns_status.py deleted file mode 100755 index bbff01f49..000000000 --- a/src/op_mode/dynamic_dns_status.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python3 - -import jinja2 -import sys -import time - -from vyos.config import Config - -cache_file = r'/var/cache/ddclient/ddclient.cache' - -OUT_TMPL_SRC = """ -{%- for entry in hosts -%} -ip address : {{ entry.ip }} -host-name : {{ entry.host }} -last update : {{ entry.time }} -update-status: {{ entry.status }} - -{% endfor -%} -""" - -if __name__ == '__main__': - # Do nothing if service is not configured - c = Config() - if not c.exists_effective('service dns dynamic'): - print("Dynamic DNS not configured") - sys.exit(0) - - data = { - 'hosts': [] - } - - with open(cache_file, 'r') as f: - for line in f: - if line.startswith('#'): - continue - - outp = { - 'host': '', - 'ip': '', - 'time': '' - } - - if 'host=' in line: - host = line.split('host=')[1] - if host: - outp['host'] = host.split(',')[0] - - if 'ip=' in line: - ip = line.split('ip=')[1] - if ip: - outp['ip'] = ip.split(',')[0] - - if 'atime=' in line: - atime = line.split('atime=')[1] - if atime: - tmp = atime.split(',')[0] - outp['time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int(tmp, base=10))) - - if 'status=' in line: - status = line.split('status=')[1] - if status: - outp['status'] = status.split(',')[0] - - data['hosts'].append(outp) - - tmpl = jinja2.Template(OUT_TMPL_SRC) - print(tmpl.render(data)) -- cgit v1.2.3