Purpose

The Python script in this repository will read all static DHCP mappings of an Ubiquiti EdgeRouter X and store them in a dnsmasq compatible hosts charge .
This means you can add newfangled DHCP static mappings to the EdgeRouter X, run this script to update the hosts file and therefor the DNS server / records. Of course you can besides schedule this handwriting by cron, so you no longer have run anything manually. Just add a new electrostatic map on the EdgeRouter X and the DNS will automatically be updated within a minute .
If configured properly, you can now add electrostatic DHCP mappings and your DNS service will resolv them properly .

Installation

Copy the script

Make certain you ‘ve a copy of the update script script and placed it on the filesystem of the EdgeRouter X.
We recommend you copy the script manually to /config/scripts/update-static-hosts.py or run :

sudo curl -o /config/scripts/update-static-hosts.py https://raw.githubusercontent.com/confirm/edgerouter-dnsmasq-updater/master/update-static-hosts.py

IMPORTANT : If you install the script in this path, the script will be left untouched by upgrade etc .

Running the script

To create or update the hosts file, you can just run the follow command :

sudo /config/scripts/update-static-hosts.py /config/user-data/hosts.static-mappings 

In case the inactive mappings changed, the script will update the define hosts file ( i.e. /config/user-data/hosts.static-mappings ) and recharge dnsmasq via SIGHUP .
IMPORTANT : The handwriting will REPLACE the define file, so you should n’t define /etc/hosts as your electrostatic hosts file.

Configuration

Persistence

Please note, everything you touch on the filesystem directly, will be lost after an OS update or a config regenerate.
To make things persistent, you ‘ve to update the EdgeRouter X config by following these steps :

  • SSH into the EdgeRouter X
  • Enter the config mode by executing configure
  • Add the commands described below
  • Commit and save them by executing commit, followed by save

This ensures that your dnsmasq & cron config stays in space, even after an OS update or config restore .
HINT : You can besides update this configuration in the EdgeRouter X WebUI under the Config Tree tab key.

Task scheduler configuration

To keep the hosts file up-to-date, you want to add a cronjob which runs the script every minute :

set system task-scheduler task update-static-hosts crontab-spec "* * * * *"
set system task-scheduler task update-static-hosts executable path /config/scripts/update-static-hosts.py
set system task-scheduler task update-static-hosts executable arguments /config/user-data/hosts.static-mappings 

DNS configuration

Before the EdgeRouter X resolves your new map hosts, you need to configure dnsmasq to use your newly created file.
We recommend the pursue configuration :

# Don't use the default /etc/hosts file.
set service dns forwarding options no-hosts

# Use /config/user-data/hosts.static-mappings for lookups.
set service dns forwarding options addn-hosts=/config/user-data/hosts.static-mappings 

# Never forward plain names (without a dot or domain part).
set service dns forwarding options domain-needed

# Never forward addresses in the non-routed address spaces.
set service dns forwarding options bogus-priv

# Add the domain to hostname lookups without a domain.
set service dns forwarding options expand-hosts
set service dns forwarding options domain=

# Don't forward the local domain.
set service dns forwarding options local=//

If you want an extra charge for host lookups ( e.g. aliases ), plainly add a new hosts file with an extra addn-hosts choice .

Leave a Reply

Your email address will not be published.