OpenWrt Forum Archive

Topic: [WORKAROUND] Resolving hostname for static DHCP leases

The content of this topic has been archived on 26 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

(Running Attitude Adjustment)

I want the router DNS to resolve DHCP leases for me. I understand I can edit /etc/ethers to do that, but I am already using the luci web interface to maintain a list of static DHCP leases in /etc/config/dhcp.

Actually, to me it would make sense to
1. Add a checkmark option to the luci DNS page: resolve names for static DHCP leases
2. Get dnsmasq to resolve those names in /etc/config/dhcp, perhaps via a script that updates /etc/ethers or some other mechanism.

Have I overlooked an easier way of doing this?
Does my suggestions make sense?

Thanks!

(Last edited by Agrajag on 11 Aug 2013, 11:23)

I would also love to see this feature for some time now... it would really make administration much simpler and would reduce errors. What a pity that the ticket is set to "feature paradise".

For the time being, setting up the static DHCP leases twice in "DHCP and DNS" and "Hostnames" is the only solultion.

Thanks macMeck.

Solution below works, but could probably be replaced by a more elegant solution involving changing /etc/init.d/dnsmasq.

1. Create script /etc/config/staticlease2dns.sh
with these contents
#!/bin/sh

# staticlease2dns.sh
# Takes dnsmasq DHCP fixed leases and
# updates list of names+ip addresses to be used by dnsmasq
# This script can be run periodically by crontab or another mechanism

# Prerequisites:
# - openwrt or other system running dnsmasq that
# - stores fixed lease info in /etc/config/ in this format
#dhcp-host=00:11:22:33:44:55,192.168.1.2,myhostname
# - has the dnsmasq addnhosts/-H parameter set to /tmp/etc/fixedleases.conf to pick up the script output

# Only take the lines containing dhcp-host; extract ip address and hostname, and write to file:
grep dhcp-host= /tmp/etc/dnsmasq.conf | cut -f 2,3 -s -d , | sed 's/,/ /g' > /tmp/etc/fixedleases.conf

# Get dnsmasq to reload hostname data by sending SIGHUP
killall -1 dnsmasq

2. Make it executable
chmod ugo+rx /etc/config/staticlease2dns.sh

3. Make it run every hour
in web interface, system, scheduled tasks,
# update DNS names with dhcp static leases list every hour
01 * * * * /etc/config/staticlease2dns.sh > /dev/null

4. set up dnsmasq to read the generated file:
in web interface, network, DHCP and DNS, Server Settings, Resolv and Hosts files
set Additional Hosts files to
/tmp/etc/fixedleases.conf

Looking also for the feature to checkbox for static leases to be resolved by DNS.

Anyone made the script allready. Or maybe implemented as a hidden feature somewhere ??

Made some adjustments in /etc/init.d/dnsmasq  version 2.71-5
OpenWrt Barrier Breaker 14.07 / LuCI Trunk (0.12+svn-r10530)

Now its working: All Static IP addresses will be resolved by DNS.

Background:
All files in /tmp/hosts will be used as hosts files and used for DNS resolution. So I made a new file in there, using the dnsmasq routine.
Because of changing the dnsmasq script (in stead of separate script), updates within the DHCP webinterface will also be exported to the new file, after submitting changes.


FROM:
HOSTFILE="/tmp/hosts/dhcp"
TO:
HOSTFILE="/tmp/hosts/dhcp"
HOSTFILE2="/tmp/hosts/staticdhcp"

Copied xappend routine: right below the xappend routine
xyappend() {
        local value="$1"

        echo "${value#--}" >> $HOSTFILE2
}

Changed route start_service() {
FROM:
        echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
TO:
        echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
        echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE2

changed in the routine:  dhcp_host_add() {
FROM:
        xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
}
TO:
        xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
        xyappend "${ip:+$ip}${name:+" "$name}"
}

(Last edited by JDD on 29 Oct 2015, 15:17)

Although this issue is rather old, it still is not solved, right? Has the approach of a separate file in /tmp/ ever been reviewd or is any other solution around?

macMeck wrote:

Although this issue is rather old, it still is not solved, right?

I never used AA or BB, but in CC all my static leases have been resolved by the DNS, no need to do anything special about it.

The discussion might have continued from here.