OpenWRT static IPv6 DUID auto-assign script - Printable Version
+- Ashus.ashus.net (
https://ashus.ashus.net)
+-- Forum: My creations (
https://ashus.ashus.net/forum-3.html)
+--- Forum: Bash scripts (
https://ashus.ashus.net/forum-17.html)
+--- Thread: OpenWRT static IPv6 DUID auto-assign script (
/thread-175.html)
OpenWRT static IPv6 DUID auto-assign script -
Ashus - 13.12.2015
The script and idea was based on
jhnphm's post
here.
The idea is to assign client's DUID addresses (IPv6) just like MAC addresses for IPv4 to the config file. The prerequisite is to have odhcpd configured and IPv6 prefixes filled for DHCP clients via LuCI. Since the DUID is required for static IPv6 to work and there are no fields in LuCI, you can use the script after the client is seen for the first time (probably configured dynamically).
Code:
#!/bin/sh
for I in $(uci show dhcp|awk -F '(\\[|\\]|\\.|=)' '{if ($2=="@host" && $5=="name") print $6}'); do # find existing static host entries and find hostnames
NAME=$(echo -ne $I | sed -r "s/'//g")
DUID=$(cat /var/hosts/odhcpd |awk -F ' ' "{if (\$1 == \"#\" && \$5 == \"$NAME\") print \$3 }"|head -n 1) # find duid for existing hostname in leases
ID=$(uci show dhcp|awk -F '(\\[|\\]|\\.|=)' "{if (\$2==\"@host\" && \$6==\"$I\") print \$3}") # Find placement of host entry
if [ "$DUID" != "0000000000000000000000000000" ]; then
echo Found: $NAME = $DUID
uci set dhcp.@host[$ID].duid=$DUID # Set DUID
fi
done
echo Commiting changes.
uci commit
killall -HUP odhcpd