From 677db2e9a64bb0816485854349f97e22ae019bde Mon Sep 17 00:00:00 2001 From: Gilles Grandou Date: Sun, 4 Oct 2020 15:45:15 +0200 Subject: [PATCH] get hosts list with both IPv6 and IPv4 addresses --- dyndomain | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/dyndomain b/dyndomain index b7d5bfd..ec05c50 100755 --- a/dyndomain +++ b/dyndomain @@ -73,13 +73,11 @@ def ping(hostname): return 0 if ret.returncode else 1 -def get_ipv6_hosts(): +def get_hosts(): r = sysbus.requete('Devices:get') - ipv6_hosts = dict() + hosts = { 'A': {}, 'AAAA': {} } for h in r['status']: - if not 'IPv6Address' in h or not h['IPv6Address']: - continue ns = {} for n in h['Names']: ns[n['Source']] = n['Name'] @@ -88,13 +86,19 @@ def get_ipv6_hosts(): hostname = ns.get('dhcp', hostname) hostname = ns.get('webui', hostname) hostname = hostname.lower() - for a in h['IPv6Address']: + for a in h.get('IPv6Address', []): if a['Scope'] != 'global' or a['Status'] != 'reachable': continue - if not hostname in ipv6_hosts: - ipv6_hosts[hostname] = [] - ipv6_hosts[hostname].append(a['Address']) - return ipv6_hosts + if not hostname in hosts['AAAA']: + hosts['AAAA'][hostname] = [] + hosts['AAAA'][hostname].append(a['Address']) + for a in h.get('IPv4Address', []): + if a['Scope'] != 'global' or a['Status'] != 'reachable': + continue + if not hostname in hosts['A']: + hosts['A'][hostname] = [] + hosts['A'][hostname].append(a['Address']) + return hosts def get_wan_addr(): @@ -134,13 +138,13 @@ def populate_zone(zone, wan_hostname, wan_addr, hosts, hosts_list, hosts_nat, do if wan_addr['ipv4'] != '': zone_add_entry(zone, 'A', wan_hostname, wan_addr['ipv4'], stamp) for host in hosts_nat: - if hosts.get(host): + if hosts['A'].get(host): zone_add_entry(zone, 'A', full_name(host, domain), wan_addr['ipv4'], stamp) if wan_addr['ipv6'] != '': zone_add_entry(zone, 'AAAA', wan_hostname, wan_addr['ipv6'], stamp) for host in hosts_list: - for addr in hosts.get(host, []): + for addr in hosts['AAAA'].get(host, []): zone_add_entry(zone, 'AAAA', full_name(host, domain), addr, stamp) @@ -298,7 +302,7 @@ if not r: print('Error: cannot authenticate on livebox') sys.exit(1) -hosts = get_ipv6_hosts() +hosts = get_hosts() wan = get_wan_addr() zone = read_zone_list(zone_filename)