perl连接SSH的代码,RH AS3,Perl 5.8时使用的
2008-01-23 16:03
176 查看
线程是10个,写在代码里面了,没有作为参数。
#!/usr/bin/perl
use threads;
use Net::SSH::Perl;
use strict;
local $SIG{ALRM} = sub { die "timeout/n"; };
sub Usage
{
print "This script used to execute command on remote server,code by xingguo/n";
print "GetPkgVer.pl <HostFile> <TimeOut> <ResultFile>/n";
exit 0;
}
if( @ARGV != 3 )
{
Usage( );
}
my $hosts_file = $ARGV[0];
my $time_out = $ARGV[1];
my $result_file = $ARGV[2];
my $user = "name";
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 = "last | grep hacker";
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 ExecCmd
{
my $host = shift;
chop( $host );
my $current_host = $host;
print "testing $host.../n";
my $output;
eval
{
alarm($time_out);
my $ssh = Net::SSH::Perl->new( $host );
$ssh->login($user, $pass);
my( $output ) = $ssh->cmd($cmd);
if( $output ne "" )
{
print ResultHandle "$host:/n$output/n";
print "$host:/n$output/n";
}
alarm(0);
};
if( $@ =~ /timeout/ )
{
print "Connect to $current_host time out./n";
}
}
while( my $host = <HostsHandle> )
{
my $total = threads -> list( );
while( 1 )
{
if( $total >= $max_thread )
{
$total = threads -> list( );
#print "Now Count: $total/n";
foreach my $join_thread ( threads -> list(threads::joinable) )
{
#print "Try to join ".$join_thread -> tid( )."/n";
$join_thread -> join( );
$total --;
}
sleep 1;
}
else
{
last;
}
}
my $work_thread = threads -> new( /&ExecCmd, $host );
}
foreach my $join_thread ( threads->list(threads::all) )
{
$join_thread -> join( );
}
print "All Done!!/n";
close( ResultHandle );
close( HostsHandle );
#!/usr/bin/perl
use threads;
use Net::SSH::Perl;
use strict;
local $SIG{ALRM} = sub { die "timeout/n"; };
sub Usage
{
print "This script used to execute command on remote server,code by xingguo/n";
print "GetPkgVer.pl <HostFile> <TimeOut> <ResultFile>/n";
exit 0;
}
if( @ARGV != 3 )
{
Usage( );
}
my $hosts_file = $ARGV[0];
my $time_out = $ARGV[1];
my $result_file = $ARGV[2];
my $user = "name";
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 = "last | grep hacker";
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 ExecCmd
{
my $host = shift;
chop( $host );
my $current_host = $host;
print "testing $host.../n";
my $output;
eval
{
alarm($time_out);
my $ssh = Net::SSH::Perl->new( $host );
$ssh->login($user, $pass);
my( $output ) = $ssh->cmd($cmd);
if( $output ne "" )
{
print ResultHandle "$host:/n$output/n";
print "$host:/n$output/n";
}
alarm(0);
};
if( $@ =~ /timeout/ )
{
print "Connect to $current_host time out./n";
}
}
while( my $host = <HostsHandle> )
{
my $total = threads -> list( );
while( 1 )
{
if( $total >= $max_thread )
{
$total = threads -> list( );
#print "Now Count: $total/n";
foreach my $join_thread ( threads -> list(threads::joinable) )
{
#print "Try to join ".$join_thread -> tid( )."/n";
$join_thread -> join( );
$total --;
}
sleep 1;
}
else
{
last;
}
}
my $work_thread = threads -> new( /&ExecCmd, $host );
}
foreach my $join_thread ( threads->list(threads::all) )
{
$join_thread -> join( );
}
print "All Done!!/n";
close( ResultHandle );
close( HostsHandle );
相关文章推荐
- MyEclipse使用Git代码托管服务——02.用ssh连接Git服务
- PL/SQL Developer结合oracle精简客户端使用配置错误代码ORA-12154: TNS: 无法解析指定的连接标识符
- Linux或者Mac系统使用SSH连接树莓派
- Ubuntu使用SSH连接
- linux之间连接—使用SSH
- 更改计算机名及使用Secure CRT ssh连接用户添加方法汇总
- VirtualBox与DockerToolBox使用轻量级Linux 系统Boot2Docker建立SSH连接
- MAC使用SSH连接IPhone
- 如何使用 RDP 或 SSH 连接到 Azure 虚拟机
- 使用paramiko模块远程连接遇到paramiko.ssh_exception.NoValidConnectionsError的解决办法
- Android----使用代码 建立gprs 闹钟 连接
- <Linux> 使用ssh,ngrok 外网远程连接linux(树莓派)
- Mac中如何使用终端SSH连接远程Linux服务器
- <Linux> 使用ssh,ngrok 外网远程连接linux(树莓派)
- <Linux> 使用ssh,ngrok 外网远程连接linux(树莓派)
- 使用JDBC连接ODBC数据库的Java代码
- 在IDEA14中使用代码连接Hbase
- 使用ssh远程连接Linux服务器开启ssh命令
- linux 下使用SSH 连接远程主机 SCP 拷贝远程文件
- 简单介绍使用 ssh 连接远程服务器运行图形界面程序