[Perl]线程telnet
时间:2006-03-13 来源:huhuegg
#!/usr/bin/perl
use strict;
use Sys::AlarmCall;
use Net::Telnet;
use Thread 'async';
use DBI;
my $obj=new Net::Telnet;
#get sign 14
$SIG{ALRM} = sub { die "Your alarm clock went off" }; ###start###
my $dsn="DBI:mysql:SERVERLIST:192.168.1.1:3306";
my $db_user="dbuser";
my $db_pass="dbpass";
my $dbh=DBI->connect($dsn,$db_user,$db_pass,{RaiseError=>1}); sub telnet {
my ($ip)=@_;
my $ok=$obj->open(Host => $ip, Port => 1020, Timeout => 2, Errmode => 'return');
if ($ok == 1) {
return "$ip \t 1020 ok!\n";
} else {
return "$ip \t 1020 false!\n";
}
} sub select_gw {
my $sth = $dbh->prepare("SELECT hostname,extip from GATEWAYLIST where inuse=1 order by hostname,extip");
$sth->execute();
my @ipaddress;
my %iphost;
my %thread;
my %stuff;
my $n=0;
while ( my @row = $sth->fetchrow_array ) {
my ($hostname,$ipaddress)=@row;
$ipaddress[$n]=$ipaddress;
$iphost{$ipaddress}=$hostname;
$n=$n+1;
}
foreach my $ipaddress (@ipaddress) {
$thread{$ipaddress}=async {
eval{
$stuff{$ipaddress}=telnet($ipaddress);
};
return $stuff{$ipaddress};
}
}
foreach my $key (keys %thread) {
$stuff{$key}=$thread{$key}->join(); #结束线程
}
foreach my $key (keys %thread) {
print $iphost{$key}, "\t", $stuff{$key};
}
} ##get tid list
#foreach $key (keys %thread) {
# print $key, " tid:", $thread{$key}->tid(), "\n";
#} print "Script START time:", `date +%Y-%m-%d_%H:%M:%S`;
&select_gw();
print "Script END time:", `date +%Y-%m-%d_%H:%M:%S`;
use strict;
use Sys::AlarmCall;
use Net::Telnet;
use Thread 'async';
use DBI;
my $obj=new Net::Telnet;
#get sign 14
$SIG{ALRM} = sub { die "Your alarm clock went off" }; ###start###
my $dsn="DBI:mysql:SERVERLIST:192.168.1.1:3306";
my $db_user="dbuser";
my $db_pass="dbpass";
my $dbh=DBI->connect($dsn,$db_user,$db_pass,{RaiseError=>1}); sub telnet {
my ($ip)=@_;
my $ok=$obj->open(Host => $ip, Port => 1020, Timeout => 2, Errmode => 'return');
if ($ok == 1) {
return "$ip \t 1020 ok!\n";
} else {
return "$ip \t 1020 false!\n";
}
} sub select_gw {
my $sth = $dbh->prepare("SELECT hostname,extip from GATEWAYLIST where inuse=1 order by hostname,extip");
$sth->execute();
my @ipaddress;
my %iphost;
my %thread;
my %stuff;
my $n=0;
while ( my @row = $sth->fetchrow_array ) {
my ($hostname,$ipaddress)=@row;
$ipaddress[$n]=$ipaddress;
$iphost{$ipaddress}=$hostname;
$n=$n+1;
}
foreach my $ipaddress (@ipaddress) {
$thread{$ipaddress}=async {
eval{
$stuff{$ipaddress}=telnet($ipaddress);
};
return $stuff{$ipaddress};
}
}
foreach my $key (keys %thread) {
$stuff{$key}=$thread{$key}->join(); #结束线程
}
foreach my $key (keys %thread) {
print $iphost{$key}, "\t", $stuff{$key};
}
} ##get tid list
#foreach $key (keys %thread) {
# print $key, " tid:", $thread{$key}->tid(), "\n";
#} print "Script START time:", `date +%Y-%m-%d_%H:%M:%S`;
&select_gw();
print "Script END time:", `date +%Y-%m-%d_%H:%M:%S`;
|
相关阅读 更多 +
排行榜 更多 +