Linux架设DDNS服务器之自动更新脚本

问题描述:客户端是动态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: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 1 } ((params=$#%2)) if [[ $# -gt 14 ]]; 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=600 # ttl interface="eth0" # 对外的联机接口! hostname=`hostname` servername=`grep 'nameserver' /etc/resolv.conf | head -n 1 |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 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;; esac done #自动查找DNS和手动都找不到DNS则退出 if [ "$servername" == "" ]; then echo "Error:Can not find the DNS Server!" exit 1 fi #检查IP合法性 checkip=`echo $newip | grep "^[0-9]"` if [ "$checkip" == "" ]; then echo "$0: The interface can't connect internet...." exit 1 fi #检测basedir目录是否存在,不存在则创建 if !([ -d $basedir ]);then mkdir -p $basedir fi #检测keyfile存在性 if !([ -f $keyfile ]);then echo "Error:$keyfile does not exist!" exit 1 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 1 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

  

  测试如下:

  

  

  

  这样的效率确实就提高了不少。

原文地址:https://www.cnblogs.com/mediciyan/p/4081188.html