欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dhcp获取ip后自动更新DNS 博客分类: linux

程序员文章站 2024-03-17 22:17:46
...
添加/etc/dhcp/dhclient-exit-hooks文件,使之可执行
内容如下
#!/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;
fi
# 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"
fi
exit 0;