随笔-260  评论-285  文章-1  trackbacks-0

 

一个脚本就实现ADSL断线重连及3322.org动态域名更新

这里是我自己编写的一个PERL脚本,5分钟去更新一次3322.org的动态域名,如果更新失败,就会主动切断PPP连接,重新开始拨号,一举两得。当然你得加个5分钟一次的CRON任务到/etc/crontab:

*/5 * * * * root perl /home/popeye/tools/update_ddns.pl

要点说明一下:

use Net::Ping;

use LWP::UserAgent;

# 自己的帐号信息

my $username = "user";

my $password = "pass";

my $yourhost = "yoururl.3322.org";

my $ipaddress = "192.168.1.1";

my $test_ip = 'www.google.com';

my $wild = ON;

# 调用拨号程序

sub dial_ppp {

# first bring up ppp0

system ("pppoe-stop");

system ("pppoe-start");

}

# 也可以用PING网站的方式,但是更新域名本身要访问网络,可以当测试条件,所以就免了

# first ping a host for testing if we're on net

#my $need_update = 1;

=pod

my $test_ping = Net::Ping->new( "icmp", 5, 64 );

if ( $test_ping->ping($test_ip) ) {

$need_update = 0; # we are OK, no need update

}else {

open (LOG, ">>/var/log/daemon.log");

print LOG "ping $test_ip failed, we'll update dns!\n";

close LOG;

}

=cut

#if ($need_update) {

open (LOG, ">>/var/log/daemon.log");

my $times = 2;

while ($times-- > 0) {

#get my address

# 从网络拿自己IP

#$ipaddress = `lynx -source http://www.3322.org/dyndns/getip`;

# 直接从PPP拿自己IP

eval {

$ipaddress = `/sbin/ifconfig ppp0 | grep 'inet addr' | awk '{print \$2}' | sed -e 's/.*://'`;

};

#拿IP不成功,说明PPP接口都没起,所以重连

if ($@) {

# dial ppp0

dial_ppp ();

next;

}

chomp($ipaddress);

#更新3322.org域名的IP

# update dns

my $url = "http://$username:$password\@members.3322.org/dyndns/update?system=dyndns&hostname=$yourhost&myip=$ipaddress&wildcard=$wild&mx=mail.exchanger.ext&backmx=NO&offline=NO";

my $cur = localtime;

#print LOG "$cur ddns need update for: $url\n";

my $ua = LWP::UserAgent->new;

$ua->timeout(20);

$ua->env_proxy;

my $response = $ua->get($url);

if ($response->is_success) {

print LOG "$cur update ddns $ipaddress succ ".$response->content; # or whatever

last;

} else { # 出错就重拨后再做

print LOG "$cur update ddns $ipaddress error ".$response->status_line;

dial_ppp ();

next;

}

}

close LOG;

#}



2007.11.24更新:下载

拨号后需要再次调用设置IPTABELS的脚本;

更新3322域名失败后,再PING一次GOOGLE,PING不通才重新拨号,避免由于3322超时导致的频繁拨号。




搬家了,影评和好玩的事跑这去了:qqmovie.qzone.qq.com
posted on 2007-11-15 14:56 云中漫步者 阅读(1205) 评论(1)  编辑  收藏 所属分类: 猎奇

评论:
# re: DIY个机器养小动物(5)--一个脚本就实现ADSL断线重连及3322.org动态域名更新 2008-12-19 00:01 | Sparkle
3322官方就有说怎么在linux下更新,用lynx一句话就行了  回复  更多评论