標籤:style blog http io color ar os 使用 sp
問題描述:用戶端是動態IP,每次連網之後要nsupdate下才可以把用戶端的hostname 與IP映射更新到DNS Server上
命令如下:
nsupdate -k K*****.key>server 192.*.*.* #dns server ip address>update delete yourfqdn A delete>update add yourFQDN 600 A your new IP
這樣的效率實在是有點低,於是我就自己寫個指令碼來更新了。直接貼上我的指令碼。指令碼:http://pan.baidu.com/s/1lhlAu
#!/bin/bash
#########################################
##
## Author:[email protected]
#########################################PATH=/sbin:/bin:/usr/sbin:/usr/binexport PATHusage(){ 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 1}((params=$#%2))if [[ $# -gt 14 ]]; then usage#elif [ $params -eq 1 ]; then# usagefi#設定預設參數值domain="swu.edu.cn" #預設測試DNS Server 地址的網域名稱basedir="/usr/local/ddns" # 基本工作目錄keyfile="$basedir"/"`ls $basedir|grep ‘.key$‘`" #公開金鑰檔案ttl=600 # ttlinterface="eth0" # 對外的聯機介面!hostname=`hostname`servername=`grep ‘nameserver‘ /etc/resolv.conf | head -n 1 |awk ‘{print $2}‘` #dns Server IPnewip=`ifconfig "$interface" | grep ‘inet addr‘ | awk ‘{print $2}‘ | sed -e "s/addr\://"` #IP地址#處理NetworkManager管理DNS Server,基本上用不到,一般在啟動NetworkManager後會自動修改resolv.confif [ "$servername" == "" ]; then servername=`nslookup $domain|grep Server|awk ‘{print $2}‘`fi#擷取使用者輸入參數,如不指定則使用預設參數while [ $# -gt 0 ]do case $1 in -i)shift;interface=$1;shift;; -d)shift;basedir=$1;shift;; -h)shift;hostname=$1;shift;; -t)shift;ttl=$1;shift;; -s)shift;servername=$1;shift;; -k)shift;keyfile=$1;shift;; -c)shift;newip=$1;shift;; -m)shift;domain=$1;shift;; *)usage;; esacdone#自動尋找DNS和手動都找不到DNS則退出if [ "$servername" == "" ]; then echo "Error:Can not find the DNS Server!" exit 1fi#檢查IP合法性checkip=`echo $newip | grep "^[0-9]"`if [ "$checkip" == "" ]; then echo "$0: The interface can‘t connect internet...." exit 1fi#檢測basedir目錄是否存在,不存在則建立if !([ -d $basedir ]);then mkdir -p $basedirfi#檢測keyfile存在性if !([ -f $keyfile ]);then echo "Error:$keyfile does not exist!" exit 1fitmpfile=$basedir/tmp.txt#如果檔案不存在,則建立if !([ -f $tmpfile ]);then touch $tmpfile #查看是否建立成功 if !([ -f $tmpfile ]);then echo "Permission Denyed,Can not touch $tmpfile in $basedir" exit 1 fifi#寫入設定檔echo "server $servername" > $tmpfileecho "update delete $hostname A " >> $tmpfileecho "update add $hostname $ttl A $newip" >> $tmpfileecho "send" >> $tmpfile#更新nsupdate -k $keyfile -v $tmpfile
測試如下:
這樣的效率確實就提高了不少。
Linux架設DDNS伺服器之自動更新指令碼