文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>通过SSH协议连接大量远程主机执行命令

通过SSH协议连接大量远程主机执行命令

时间:2009-01-06  来源:xiaowei

Net::SSH::Perl模块是自己实现了SSH协议,因此是速度最快的一个模块。多进程,多线程均可安全使用。但是使用中发现有一个bug,就是在 登录某些机器的时候,登录会一直挂起,进程不能继续往下执行,这里使用ALARM超时都没有作用,确实很奇怪。挂起的时候,手动输入回车就可以继续,此时 报错为“Connection closed by remote host. at /usr/local/lib/perl5/site_perl/5.8.8/Net/SSH/Perl/Auth/ChallengeResponse.pm line 61”,就这个问题我给作者写过邮件,但是没有回复。我通过设置对象的option,禁止这种验证方式,还是没有效果,因此目前我的代码还存在这个隐患, 代码稍后附上。

#!/usr/bin/perl

use warnings;
use strict;
use Net::SSH::Perl;
use Getopt::Std;
use Parallel::ForkManager;

my %options_hash;
getopts( 'h:d:c:r:', \%options_hash );

if ( (not defined($options_hash)) or (not defined($options_hash))  or (not defined($options_hash)) )
{
        &Usage( );
        exit(-1);
}

my $time_out = $options_hash || 4;
my $hosts_file = $options_hash;
my $cmd = $options_hash;
my $result = $options_hash;

my $user;
my $pass;
my $max_process = 20;

print "Enter your account: ";
$user = <STDIN>;
chomp( $user );

print "Password: ";
$pass = <STDIN>;
chomp( $pass );

open( HostsHandle, "< $hosts_file" ) || die "Open hosts list error...\n";
open( ResultHandle, ">> $result" ) || die "Create result file error...\n";

my $pm = new Parallel::ForkManager( $max_process );
while( my $host = <HostsHandle> )
{
        $pm->start and next; # do the fork and skip parent

        ExecCmd( $host );

        $pm->finish; # do the exit in the child process      
}

$pm->wait_all_children;

close( HostsHandle );
close( ResultHandle );

sub ExecCmd
{
        my $host = shift;
        chomp( $host );
      
        print "testing $host...\n";

        my $ssh;
        my $login;
      
        eval
        {
                local $SIG = sub { die "timeout\n"; };

                alarm $time_out;
              
                $ssh = Net::SSH::Perl->new( $host );
      
                $login = $ssh->login($user, $pass);
              
                alarm 0;
        };

        if( $@ )
        {
                if( $@ =~ /timeout/ )
                {
                        print "Connect to $host time out.\n";

                        return(-1);
                }
                else
                {
                        print "Connect to $host error.\n";
                        return(-1);
                }
        }

        #if( not defined $ssh )
        #{
        #        print "Connect to $host error.\n";
        #
        #        return(-1);
        #}
      
        #if( not defined $login )
        #{
        #        print "Login to $host error.\n";
        #
        #        return(-1);
        #}

        my ( $output ) = $ssh->cmd($cmd);

        if( $output )
        {
                print "$host:\n$output\n";
                print ResultHandle "$host:\n$output\n";
        }
}

sub Usage
{
        print "\nThis script used to execute command on remote server,code by yunshu\n";
        print "$0 -h <HostFile> -c <Cmd> -r <ResultFile> -d [TimeOut]\n";
        print "Options:\n";
        print "        <HostFile>     ip lists,one ip address each line.\n";
        print "        [TimeOut]      ssh connection time out,default 4 second.\n";
        print "        <Cmd>          what cmd will be executed on remote host,use quotation mark if some special character.\n";
        print "        <ResultFile>   save result in this file.\n\n";
}
相关阅读 更多 +
排行榜 更多 +
别惹神枪手安卓版

别惹神枪手安卓版

冒险解谜 下载
坦克战争世界

坦克战争世界

模拟经营 下载
丛林反击战

丛林反击战

飞行射击 下载