Chinaunix首页 | 论坛 | 博客
  • 博客访问: 404244
  • 博文数量: 80
  • 博客积分: 8021
  • 博客等级: 中将
  • 技术积分: 1075
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-08 10:36
文章分类

全部博文(80)

文章存档

2010年(3)

2009年(25)

2008年(52)

我的朋友

分类:

2009-01-06 10:25:16

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 = ;
chomp( $user );

print "Password: ";
$pass = ;
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 = )
{
        $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 -c -r -d [TimeOut]\n";
        print "Options:\n";
        print "             ip lists,one ip address each line.\n";
        print "        [TimeOut]      ssh connection time out,default 4 second.\n";
        print "                  what cmd will be executed on remote host,use quotation mark if some special character.\n";
        print "           save result in this file.\n\n";
}
阅读(1273) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~