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 diff --git a/usg-easy-dns.py b/usg-easy-dns.py index 5eba599..b502b80 100755 --- a/usg-easy-dns.py +++ b/usg-easy-dns.py @@ -112,14 +112,27 @@ 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 - - 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 + 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))