首页 > 代码库 > Linux架设DDNS服务器之自动更新脚本

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/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技术网
  • 本文链接:http://www.ahlinux.com/shell/7971.html

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