一个用来检查FreeBSD 中ADSL 是否正常连线的工具

使用前必须确定你的系统已经安装了fping

/usr/ports/net/fping

在/etc/crontab中加入下面的内容,即可每隔20分钟检查一次


*/20 * * * * root /usr/local/chkadsl/adsl.pl > /dev/null 2>&1

修改脚本,将里面的ip_address1,ip_address2,ip_address3 改为你用来检查IP

 

下载

下面是脚本的内容

#!/usr/bin/perl -w
#
#adsl.pl
#name:check adsl connect
#author: fengyong 
#2006-12-20
use strict;
use vars qw ($fping @ips);

my $fping = "/usr/local/sbin/fping";
my @ips=("ip_address1","ip_address2","ip_address3");

#this is test ip,uncomment it when the script is ok. 
#my @ips=("1.1.1.1","1.2.4.5","2.1.1.1");

sub check_ip {
	my $ip=shift;
	my $ret=`$fping -t 10 $ip`;
	chomp $ret;
	if ($ret =~/$ip is alive/){
		return 1;
	}else{
		return 0;
	}
}
		
sub chk_adsl {
	my $counter=0;
	foreach my $ip(@ips){
		if (&check_ip($ip)){
			$counter++;
		}
	}
	if ($counter > 2){
		return 0;
	}else{
		return 1;
	
	}
}

sub reconnect {
	system ( "/usr/bin/killall ppp");
	system ("/usr/sbin/ppp -ddial -nat adsl");
}

if (&chk_adsl){
	&reconnect;
}