#!/usr/bin/perl
use threads;
use Net::SSH::Perl;
use strict;
my $current_host;
local $SIG = sub { print "connect $current_host time out/n";return; };
sub Usage
{
print "This script used to get the package's version from remote server,code by xingguo/n";
print "GetPkgVer.pl <HostFile> <Name> <TimeOut> <ResultFile>/n";
exit 0;
}
if( @ARGV != 4 )
{
Usage( );
}
my $hosts_file = $ARGV[0];
my $package = $ARGV[1];
my $time_out = $ARGV[2];
my $result_file = $ARGV[3];
my $user = "user";
my $pass = "password";
if( -e $result_file )
{
print "Result file exists,please change another one/n";
exit 0;
}
my $max_thread = 10;
my @thread_array;
my $cmd = "yinst list $package";
system( "touch $result_file" );
open( ResultHandle, ">> $result_file" ) || die "Open result file error.../n";
open( HostsHandle, "< $hosts_file" ) || die "Open hosts list error.../n";
sub GetVersion
{
my $host = shift;
chop( $host );
$current_host = $host;
print "/nDebug:Try to get package version from $host.../n";
my $output;
eval
{
alarm $time_out;
my $ssh = Net::SSH::Perl->new( $host );
$ssh->login($user, $pass);
my( $output ) = $ssh->cmd($cmd);
alarm 0;
if( $output =~ /$package/ )
{
print ResultHandle "$host/t$output/n";
print "$host/t$output/n";
}
};
}
my $current_thread = 0;
while( my $host = <HostsHandle> )
{
if( $current_thread >= $max_thread )
{
foreach my $thread( @thread_array )
{
$thread -> join( );
}
$current_thread = 0;
}
$thread_array[$current_thread] = threads -> new( /&GetVersion, $host );
$current_thread ++;
}
close( ResultHandle );
close( HostsHandle );