get hosts list with both IPv6 and IPv4 addresses

This commit is contained in:
Gilles Grandou 2020-10-04 15:45:15 +02:00
parent 893327c31d
commit 677db2e9a6

View File

@ -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)