闲情逸致 http://EZ38.com
分类: LINUX
2010-03-05 16:17:19
使用一个合适的动态域名可以省却很多麻烦事,其中一项就是远程控制家里的ADSL拨 号的主机,Windows下面的动态域名服务商会提供客户端软件,输入用户名和密码就可以了,Unix/Linux的X环境下,也有一些,那么,如果是纯 粹的命令行终端,可以使用shell脚本实现命令行的动态域名么?回答当然是肯定的,下面就本人的成功经验,和大家分享。 因为我使用的是ChangeIP的免费账号,网址:,根据提示,自行注册一个账户,假设用户名:user,密码:pass,并添加至少一个免费域名,比如:使用的是xxxxxx.ddns.us,会出现在你的账户里面,就像这样: ------------------------------------------------------------ Free Domain Names Action Serial Status xxxxxx.ddns.us Edit Delete 1 Active ------------------------------------------------------------ 因为申请的主要流程随着时间的变化可能有不同,按照网站的指示来做,通常不会有什么问题 注册完毕账号和域名,下一步是要验证注册的有效性,使用一个可以上网的Windows\Linux\Unix\OpenBSD,输入ping ------------------------------------------------------------ # ping PING www-china.l.google.com (64.233.189.104): 56 data bytes 64 bytes from 64.233.189.104: icmp_seq=0 ttl=243 time=242.907 ms 64 bytes from 64.233.189.104: icmp_seq=1 ttl=243 time=303.746 ms ------------------------------------------------------------ 如上返回则表示本机和互联网连接可用 现在返回ChangeIP的个人账户页面,按下Update DDSN Set后面的1,或者2,因为每个账户可以设置2组IP,通常个人使用1组就够了,这时候会请求你的用户名和密码,输入正确后,网页显示: ------------------------------------------------------------ 200 Successful Update (Address Used: xxx.xxx.xxx.xxx) Updated target: *1 Updated 0 host records Updated 0 zone serial numbers Reviewed 0 possible records Total updates: 80 Lockout counter: 1 out of 60 Lockout reset: 61 mins Elapsed time: 0.02 seconds NIC version: 2.67 For XML output add &xml=1 ------------------------------------------------------------ 再输入ping xxxxxx.ddns.us,如果可用,则返回: ------------------------------------------------------------ # ping xxxxxx.ddns.info PING xxxxxx.ddns.info (59.37.71.86): 56 data bytes 64 bytes from 59.37.71.86: icmp_seq=0 ttl=57 time=215.884 ms 64 bytes from 59.37.71.86: icmp_seq=1 ttl=57 time=252.498 ms ------------------------------------------------------------ 本人的使用的环境是OpenBSD 4.4操作系统,用纯命令行终端,减小资源消耗 在看下去之前,你必须安装一个支持https协议的命令行浏览器,本人选择的是curl,嗯,接下来就是重点: -------------------------------------------------------------------- #!/bin/sh #本脚本从Eric Ste-Marie的脚本变种而来 #原脚本: PATHTOCURL=/usr/local/bin/curl PATHTOLOGFILE=/home/DDNS/DDNS.log INTERFACE=rl0 USER=user PASSWORD=pass umask 177 $PATHTOCURL -k -u $USER:$PASSWORD 1>>$PATHTOLOGFILE 2>&1 date>>$PATHTOLOGFILE echo "-----------------------------------\n">>$PATHTOLOGFILE -------------------------------------------------------------------- 和原脚本相比,更新点: 1、使用了cURL的命令行浏览器,因为我的lynx不支持https,OpenBSD4.4里面的https的Package补丁我也不知道打哪一个,索性使用了curl,Ver 7.18.2,直接支持https,哈,懒人就是用懒招。 2、Eric Ste-Marie发布的脚本是用于本机拨号的,我的使用环境是路由器拨号,所以,原来有关本机的IP变化检测被我删掉了,剩下的只是单纯的Update脚本。 将脚本保存成文件ddns.sh,chmod 700,使用crontab,增加一条 #DDNS Server Update 15 * * * * /home/thomasdu/DDNS/ddns.sh 然后电脑在每个小时15分的时候,更新自己的IP,通常来说,在ADSL24小时断线一次的情况下,本脚本满足一般性需求,偶尔ssh\ftp等等,还是非常方便的,Enjoy! #!/bin/sh ############################################################################################################# ## Author: Eric Ste-Marie ## Email: estemari@sympatico.ca ## Date: December third 2000 ## Note: This script falls under Gnu General Public Licence. ## go to "" for more information. ## basically, it tells you that you have the legal permission to copy, distribute and/or modify ## the software. It also tells you that it comes with NO WARRANTY. ## ## ###################################################### ## Script to update changeIP dynamic dns to include as a cron job. ## i.e.: "*/5 * * * * /sbin/SCRIPTNAME" to run it every five minutes ## It uses lynx patched with SSL ## put the script where you want it. ## Adjust the environment variables to suit your needs. ## PATHTOLYNX= patch to lynx. You can try "which lynx" on the command line ## to find out ## PATHTOIFCONFIG = path to the ifconfig command, usually /sbin/ifconfig ## PATHTOCURRENTIP = file where to store the most recent ip ## PATHTOLOGFILE = file where to log information ## INTERFACE = Network interface to configure. e.g. ppp0, eth0, ppp ## (you can find out with ifconfig -a) ## USER = Your changeIP username ## PASSWORD = Your changeIP password. Note that if you want, you could ## put the password in something like /etc/changeip/secret ## and set PASSWORD=`cat /etc/changeip/secret` Don't forget ## to make this script and that file readable only by root ## (chmod 700) for obvious reasons. ## CMD = changeIP commmand. update for most of us ## SET = 1 ## OFFLINE = 0 (or one to put your domain name to your offline ip. ## ## ## ## Don't forget to chmod 700 the script as you have the password in there. ## (chmod 700 /sbin/SCRIPTNAME) ## Don't forget to chown root:root the script or bin:bin . ## (chown root:root /sbin/SCRIPTNAME) ## ## After you run the script for the first time, remember to verify that the ## log files are NOT readable by anybody else than root, especially if you ## use debug mode (set -x) # set -x #uncomment to run in debug mode ###############Script variables to set################################## PATHTOLYNX=/usr/bin/curl PATHTOIFCONFIG=/sbin/ifconfig PATHTOCURRENTIP=/var/log/changeip/ip PATHTOLOGFILE=/var/log/changeip/cip.log INTERFACE=tun0 USER=fanjunfeng #might be case sensitive PASSWORD=? #might be case sensitive CMD=update SET=1 OFFLINE=0 ########################################################################## umask 177 #To set file creation to 600 CURRENTIP=`ifconfig $INTERFACE |grep inet| awk '{print $2}'` grep $CURRENTIP $PATHTOCURRENTIP 1>/dev/null 2>&1 if [ $? -ne 0 ]; then $PATHTOLYNX -dump -accept_all_cookies "" 1>>$PATHTOLOGFILE 2>&1 CURRENTIP=`ifconfig $INTERFACE |grep inet| awk '{print $2}'` echo "$CURRENTIP" > $PATHTOCURRENTIP fi date>>$PATHTOLOGFILE echo "-----------------------------------\n">>$PATHTOLOGFILE #15 * * * * /home/ddns.sh curl -k -u fanjunfeng:? \&set=1\&offline=0 这样做是成功的,我刚试过 |