Automatically update DNS after dhcp obtains ip

Add the /etc/dhcp/dhclient-exit-hooks file to make it
executable as follows
#!/bin/bash
echo ""
echo "dhclient-exit-hooks running..."
echo "reason is ${reason}"
echo "interface is ${interface}"
# only execute on the primary nic
if [ "$interface" != "eth0" ]
then
    exit 0;
be
# when we have a new IP, perform nsupdate
if [ "$reason" = "BOUND" ] || [ "$reason" = "RENEW" ] ||
   [ "$reason" = "REBIND" ] || [ "$reason" = "REBOOT" ]
then
    echo "new_ip_address: ${new_ip_address}"
    host=$(hostname | cut -d'.' -f1)
    domain=$(hostname | cut -d'.' -f2- -s)
    IFS='.' read -ra ipparts <<< "$new_ip_address"
    ptrrec = "$ {ipparts [3]}. $ {ipparts [2]}. $ {ipparts [1]}. $ {ipparts [0]}. in-addr.arpa"
    nsupdatecmds="/tmp/nsupdate"
    resolvconfupdate="/tmp/resolvconfupdate"
    echo "updating resolv.conf"
    grep -iv "search" /etc/resolv.conf > "$resolvconfupdate"
    echo "search $domain" >> "$resolvconfupdate"
    cat "$resolvconfupdate" > /etc/resolv.conf
    echo "Attempting to register $host.$domain and $ptrrec"
    {
        echo "update delete $host.$domain a"
        echo "update add $host.$domain 600 a $new_ip_address"
        echo "send"
        echo "update delete $ptrrec ptr"
        echo "update add $ptrrec 600 ptr $host.$domain"
        echo "send"
    } > "$nsupdatecmds"
    nsupdate  "$nsupdatecmds"
be
exit 0;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326492863&siteId=291194637