Linux setup DDNS server automatic Update script

Source: Internet
Author: User

Problem Description: The client is a dynamic IP, after each network to Nsupdate to the client's hostname and IP map to update to the DNS server

The command is as follows:

  

Nsupdate-k k*****. Key192. *.*.*        #dns server IP address>Update Delete yourfqdn A Delete A your new IP

This kind of efficiency is a little low, so I wrote a script to update. Paste my script directly. Script: 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 Testdomai N]" Echo "Default:" Echo "-I eth0-d/usr/local/ddns-t 600-k/usr/local/ddns/*.key" Echo "" Echo "Notice: If your own host is DNS Server, then you cannot change your own hostname with IP" Echo "If you don't know what your DNS server is, add the-m parameter, followed by the test domain name, 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 add Ress,eg:$0-m swu.edu.cn"Exit1} ((params=$#%2))if[[$#-GT -]]; Thenusage#elif[$params-eq 1]; Then# Usagefi#设置默认参数值domain="swu.edu.cn"the domain name of the #默认测试DNS Server address Basedir="/usr/local/ddns"# Basic working directory KeyFile="$basedir"/"' ls $basedir |grep '. key$ '"#公钥文件ttl= -# Ttlinterface="eth0"# External Online interface! hostname=`hostname' servername=`grep 'nameserver'/etc/resolv.conf |Head-N1|awk '{print $}'' #dns Server ipnewip=`ifconfig "$interface"|grep 'inet Addr'|awk '{print $}'|sed-E"s/addr\://"' #IP地址 # processing NetworkManager Management DNS Server, basically not used, generally after starting NetworkManager will automatically modify resolv.confif["$servername"==""]; Thenservername= ' Nslookup $domain |grepserver|awk '{print $}'`fi#获取用户输入参数, default parameters are used if not specified while[$#-GT0 ] Do Case$1 inch-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;d omain=$1;Shift;; *) usage;; Esac Done#自动查找DNS和手动都找不到DNS则退出if["$servername"==""]; Then Echo "Error:can not find the DNS server!"Exit1fi#检查IP合法性checkip=`Echo$newip |grep "^[0-9]"`if["$checkip"==""]; Then Echo "$0:the interface can ' t connect the Internet ...."Exit1fi#检测basedir目录是否存在, does not exist then createsif! ([-D $basedir]); Then mkdir-P $basedirfi#检测keyfile存在性if! ([-F $keyfile]); Then Echo "Error: $keyfile does not exist!"Exit1fitmpfile= $basedir/tmp.txt# If the file does not exist, createif! ([-F $tmpfile]); Then Touch$tmpfile #查看是否创建成功if! ([-F $tmpfile]); Then Echo "Permission Denyed,can not touch $tmpfile in $basedir"Exit1 fifi#写入配置文件Echo "Server $servername">$tmpfileEcho "Update Delete $hostname A">>$tmpfileEcho "Update Add $hostname $ttl A $newip">>$tmpfileEcho "Send">>$tmpfile # Update Nsupdate-K $keyfile-V $tmpfile

  

The test is as follows:

  

  

  

This efficiency has indeed improved a lot.

Linux setup DDNS server automatic Update script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.