From 8f4b6e3b1d09eeeed63e8c9d91da173b49343aca Mon Sep 17 00:00:00 2001 From: Fraddles <65753186+Fraddles@users.noreply.github.com> Date: Mon, 19 Sep 2022 17:39:22 +1000 Subject: [PATCH 1/3] Update usg-easy-dns.py Query DHCP leases for clients that only have an alias not an actual fixed IP. --- usg-easy-dns.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/usg-easy-dns.py b/usg-easy-dns.py index 5eba599..b1f0356 100755 --- a/usg-easy-dns.py +++ b/usg-easy-dns.py @@ -114,12 +114,22 @@ def get_fixed_ips(self, site=DEFAULT_SITE): for client in self.get_clients(): if 'fixed_ip' not in client: continue - - ip = client['fixed_ip'] + name = client['name'] if 'name' in client else client['hostname'] name = re.sub(pattern=r'[\s_-]+', repl='-', string=name, flags=re.IGNORECASE) name = re.sub(pattern='[^0-9a-z-]', repl='', string=name, flags=re.IGNORECASE) + if client['use_fixedip']: + ip = client['fixed_ip'] + LOGGER.debug("Got Fixed IP for %s", name) + else: + with open("/config/dnsmasq-dhcp.leases","r") as dhcpfile: + for line in dhcpfile: + if client['mac'] in line: + ip = line.split(" ")[2] + LOGGER.debug("Got DHCP IP for %s", name) + break + LOGGER.debug('Adding host %s with IP address %s', name, ip) clients.append((ip, name)) From 867a961073f2cdd31082ea181d213f2cba0c8927 Mon Sep 17 00:00:00 2001 From: Fraddles <65753186+Fraddles@users.noreply.github.com> Date: Mon, 19 Sep 2022 18:25:07 +1000 Subject: [PATCH 2/3] Update READ.md to reflect script updates --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 17f92b8..b3610dc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +DISCLAIMER: First github adventure... NFI how to properly attribute this! + Introduction ============ @@ -6,7 +8,7 @@ Purpose This project / repository contains a Python script and description on how to make the [UniFi Security Gateway](https://www.ui.com/unifi-routing/usg/) DNS service automatically resolve aliases (and only aliases) specified in the UniFi controller. -**This means you only have to define a client alias & a fixed IP address in the UniFi UI and the DNS server is automatically updated with that entry!** +**This means you only have to define a client alias and/or a fixed IP address in the UniFi UI and the DNS server is automatically updated with that entry!** Rationale --------- @@ -91,8 +93,8 @@ DNS Configuration Of course the DNS server needs to be informed about this new file as well. Here's an example for that configuration: ``` -# Don't use the default /etc/hosts file. -set service dns forwarding options no-hosts +# Don't use the default /etc/hosts file. I did not use this. +#set service dns forwarding options no-hosts # Use the generated /config/user-data/hosts instead. set service dns forwarding options addn-hosts=/config/user-data/hosts From c632653e9ea8c72ad72cb5c4e77648dcab71675c Mon Sep 17 00:00:00 2001 From: Fraddles <65753186+Fraddles@users.noreply.github.com> Date: Sun, 2 Oct 2022 11:02:13 +1100 Subject: [PATCH 3/3] Improve handling for clients with no IP details Change from 'fixed_ip' to 'use_fixedip' field in client record to handle configured clients without a saved fixed IP. Added handling for configured clients that have no current fixed or DHCP IP, ie offline in the case of DHCP clients. --- usg-easy-dns.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/usg-easy-dns.py b/usg-easy-dns.py index b1f0356..b502b80 100755 --- a/usg-easy-dns.py +++ b/usg-easy-dns.py @@ -112,7 +112,7 @@ def get_fixed_ips(self, site=DEFAULT_SITE): clients = [] for client in self.get_clients(): - if 'fixed_ip' not in client: + if 'use_fixedip' not in client: continue name = client['name'] if 'name' in client else client['hostname'] @@ -128,7 +128,10 @@ def get_fixed_ips(self, site=DEFAULT_SITE): if client['mac'] in line: ip = line.split(" ")[2] LOGGER.debug("Got DHCP IP for %s", name) - break + break + else: + LOGGER.debug("No IP found for %s, skipping", name) + continue LOGGER.debug('Adding host %s with IP address %s', name, ip) clients.append((ip, name))