1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
   | #!/usr/bin/perl -w
 
use DBI;
use Date::Format;
use Net::Telnet;
sub testaddress($$);
sub update_ip($);
sub remove_ip($);
#Params=================
my $pingtimeout= '25'; #timeout 25 secs..
my $iplimit = '600'; #number of address to test.
my $sqlhost="localhost"; # mysql server
my $sqluser = ""; #mysql username 
my $sqlpass = ""; #root passwd
my $sqldb = ""; #ip adress db
my $sqltable = "listeIp"; 
#Fin params=============
my $dbh = DBI->connect("DBI:mysql:database=$sqldb:host=$sqlhost",$sqluser,$sqlpass) or die ("Cannot connect to db");
my $getip = $dbh->prepare("select Ipadress, port from listeIp limit $iplimit");
$getip->execute() or die "Cannot get IP list. \n";
while(my $result = $getip->fetchrow_hashref()){
    my $ip = $result->{"Ipadress"};
    my $port = $result->{"port"};
    my $res = testaddress($ip,$port);
    if(!$res){
	remove_ip($ip);
    }
    else{
	update_ip($ip);
    }
}
$dbh->disconnect();
exit;
 
sub testaddress($$){
    my $ok=0;
    my ($ip,$port)=@_;
    $p = new Net::Telnet (Host    => $ip,
			  Port    => $port,
			  Timeout => $pingtimeout,
			  Errmode => "return");
    return($p);
}
 
sub remove_ip($){
    my ($ip)=@_;
    my $rem = $dbh->prepare("delete from listeIp where Ipadress='$ip'");
    $rem->execute() or die "Cannot delete $ip from IP table\n";
}
sub update_ip($){
    my ($ip)=@_;
    my $update_lc = $dbh->prepare("update listeIp set LastCheck=Now() where Ipadress='$ip'");
    $update_lc->execute() or die "Cannot update LastCheck value for $ip";
    my $update_stat = $dbh->prepare("update listeIp set Status='Verified' where Ipadress='$ip'");
    $update_stat->execute() or die "Cannot update Status for $ip";
} | 
Partager