Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic IP handling #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DISCLAIMER: First github adventure... NFI how to properly attribute this!

Introduction
============

Expand All @@ -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
---------
Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions usg-easy-dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down