
问题描述:客户端是动态IP,每次连网之后要nsupdate下才可以把客户端的hostname 与IP映射更新到DNS Server上
命令如下:
nsupdate -k K*****.key
>server .*.*.* #dns server ip address
>update delete yourfqdn A delete
>update add yourFQDN A your new IP
这样的效率实在是有点低,于是我就自己写个脚本来更新了。直接贴上我的脚本。脚本下载地址:http://pan.baidu.com/s/1lhlAu
#!/bin/bash
#########################################
##
## Author:Medici.Yan@gmail.com
#########################################
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH
usage(){
echo " Usage:$0 [-i interface] [-d basedir] [-h hostname] [-t ttl] [-s servername] [-k keyfile] [-c ClientIP] [-m testdomain]"
echo " Default:"
echo " -i eth0 -d /usr/local/ddns -t 600 -k /usr/local/ddns/*.key"
echo ""
echo " Notice: 如果你自己的主机是DNS Server,那么你不能改变你自己的 hostname 与 IP"
echo " 如果你不知道你的DNS Server是什么,就加上-m 参数,后面是测试的域名,eg:$0 -m swu.edu.cn "
echo " Notice: If your PC is your DNS Server, you can't change your hostname and IP"
echo " If you don't know your DNS Server Address, you can use the param [-m TestDomain] to get the right server address,eg:$0 -m swu.edu.cn "
exit
} ((params=$#%))
if [[ $# -gt ]]; then
usage
#elif [ $params -eq 1 ]; then
# usage
fi #设置默认参数值
domain="swu.edu.cn" #默认测试DNS Server 地址的域名
basedir="/usr/local/ddns" # 基本工作目录
keyfile="$basedir"/"`ls $basedir|grep '.key$'`" #公钥文件
ttl= # ttl
interface="eth0" # 对外的联机接口!
hostname=`hostname`
servername=`grep 'nameserver' /etc/resolv.conf | head -n |awk '{print $2}'` #dns Server IP
newip=`ifconfig "$interface" | grep 'inet addr' | awk '{print $2}' | sed -e "s/addr\://"` #IP地址 #处理NetworkManager管理DNS Server,基本上用不到,一般在启动NetworkManager后会自动修改resolv.conf
if [ "$servername" == "" ]; then
servername=`nslookup $domain|grep Server|awk '{print $2}'`
fi
#获取用户输入参数,如不指定则使用默认参数
while [ $# -gt ]
do
case $ in
-i)shift;interface=$;shift;;
-d)shift;basedir=$;shift;;
-h)shift;hostname=$;shift;;
-t)shift;ttl=$;shift;;
-s)shift;servername=$;shift;;
-k)shift;keyfile=$;shift;;
-c)shift;newip=$;shift;;
-m)shift;domain=$;shift;;
*)usage;;
esac done
#自动查找DNS和手动都找不到DNS则退出
if [ "$servername" == "" ]; then
echo "Error:Can not find the DNS Server!"
exit
fi #检查IP合法性
checkip=`echo $newip | grep "^[0-9]"` if [ "$checkip" == "" ]; then
echo "$0: The interface can't connect internet...."
exit fi
#检测basedir目录是否存在,不存在则创建
if !([ -d $basedir ]);then
mkdir -p $basedir
fi
#检测keyfile存在性
if !([ -f $keyfile ]);then
echo "Error:$keyfile does not exist!"
exit
fi tmpfile=$basedir/tmp.txt
#如果文件不存在,则创建
if !([ -f $tmpfile ]);then
touch $tmpfile
#查看是否创建成功
if !([ -f $tmpfile ]);then
echo "Permission Denyed,Can not touch $tmpfile in $basedir"
exit
fi
fi
#写入配置文件
echo "server $servername" > $tmpfile
echo "update delete $hostname A " >> $tmpfile
echo "update add $hostname $ttl A $newip" >> $tmpfile
echo "send" >> $tmpfile
#更新
nsupdate -k $keyfile -v $tmpfile
测试如下:
这样的效率确实就提高了不少。