2015-04-22 32 views
0

我遇到Net :: SSH2 :: Channel的问题。我正在使用Oracle LINUX 2.6.39-400.214.5.el6uek.x86_64,使用perl-5.10.1-136.el6.x86_64。perl net的问题:: SSH2 ::通道

远程服务器运行Debian 3.2.60-1 + deb7u3 x86_64。

perl代码将被用作bash脚本中的函数。现在,这是一本简单的perlscript。

我对编程perl不是很有经验。到目前为止,我只修改了现有的perlscript。

使用我的testscript,我可以使用SSH2登录到远程服务器。

然后,我能够使用$chan->shell(<conmmand>); 执行一个且只有一个命令第二个命令似乎失败,即使它将作为脚本中的第一个shell命令成功执行。 - >虽然$chan->exec();据说只支持通道上的一个命令,但$chan->shell();应该可以在同一通道上多次使用。 用$chan-->close;关闭频道并重新打开频道也无济于事。 - >如何解释$ssh2->error;的输出?

我的第二个问题是:究竟阻碍了什么? 在ssh连接(ssh->blocking();)和通道($chan->blocking(0);)上使用阻塞之间有什么区别?我见过两个。 对我而言,在频道上使用阻止或不使用频道没有任何效果。

最后,我不知道如何获得执行命令的返回值以及如何以某种方式读取它,以至于我不必事先知道它的长度。 (在testscript中使用的目录/ tmpp不存在使用现有目录不会改变任何内容 我的输出始终与输出的前65个字符相同uname -a 当我设置bufleng =70时,脚本挂起。在这里,事情似乎是完全错误的。

任何建议是极大的欢迎。 谢谢您的支持。

再见 拉尔夫


我的脚本如下:

#!/usr/bin/perl 
 

 
    use warnings; 
 
    use strict; 
 
    use Net::SSH2; 
 
    use Data::Dumper; 
 

 
    # Handling options 
 
    my $host = shift; 
 
    my $user = shift; 
 
    my $passwd = shift; 
 
    my $command = shift; 
 
    my $destination = shift; 
 
    #my $source = $ARGV; 
 

 
    # Defining variables 
 
    my $LEC=""; 
 

 
    #my $scp_session=new Net::ssh2 (Errmode=>'return', 
 
    my $ssh2=Net::SSH2 -> new(); 
 
    $ssh2->debug(1); 
 
##print Dumper($ENV); 
 

 
    ### Connecting (10) 
 
    #die "can\'t connect to ${host}, Error 10" unless $ssh2->connect($host); 
 
    #print "Connected to ${host}\n"; 
 
    $ssh2->connect($host); 
 
    if ("$?"==0) { 
 
    print "Connected to ${host}\n"; 
 
    } else { 
 
    print "can\'t connect to ${host}, Error 10\n"; 
 
    #print $ssh2->error; 
 
    print "\n"; 
 
    } 
 

 
    ### Logging in (20) 
 
    #die "can't authenticate as ${user}" unless 
 
    #$ssh2->auth(username => '${user}',password => '${passwd}'); 
 
    $ssh2->auth_password(${user},${passwd}); 
 
    # print $ssh2->error; 
 
    # #print "\n"; 
 
    if ("$?"==0) { 
 
    print " Authenticated as ${user}\n"; 
 
    } else { 
 
    print $ssh2->error; 
 
    print " Authentication failed\n"; 
 
    exit 20 
 
    } 
 

 
print " ## step 0\n"; 
 

 
    ### workload (30) 
 
    my $chan = $ssh2->channel(); 
 
    print $ssh2->error; 
 
    print "\n ## step 2\n"; 
 
    #$chan->blocking(0); 
 
    # print $ssh2->error; 
 
    # print "\n ## step 3\n"; 
 
    #$chan->shell('set cli-parameters console pager disabled'); 
 
    # print $ssh2->error; 
 
    # print "\n ## step 4\n"; 
 
    # $chan->close; 
 
    # my $chan = $ssh2->channel(); 
 
    ### testing 
 
    $chan->shell('ls -la'); 
 
    print $ssh2->error; 
 
    print "\n ## step 5\n"; 
 
    my $buflen = 65; 
 
    my $buf1 = '0' x $buflen; 
 
    $chan->read($buf1, $buflen); 
 
    print $buf1,"\n"; 
 
    $chan1->shell(${command}); 
 
    print $ssh2->error; 
 
    print "\n ## step 6\n"; 
 
    #my $buflen = 70; 
 
    print $ssh2->error; 
 
    print "\n ## step 7\n"; 
 
    my $buf2 = '0' x $buflen; 
 
    print $ssh2->error; 
 
    print "\n ## step 8\n"; 
 
    $chan->read($LEC, $buflen); 
 
    print $ssh2->error; 
 
    print "\n ## step 9\n"; 
 
    print "${command}:\n", $buf1,"\n"; 
 
    print $ssh2->error; 
 
    print "\n ## step 10\n"; 
 
    $chan->shell('exit'); 
 
    print $ssh2->error; 
 
    print "\n ## step 11\n"; 
 

 

 
    ### Logging off and disconnecting (90) 
 
    $ssh2->disconnect($host); 
 
    #print $ssh2->error; 
 
    #print "\n"; 
 

 

 
    #($ssh2->auth_password($user,$passwd)) { 
 
     #print "\n Executing command...\n"; 
 
     my $cmd = "ls"; 
 
     #print " ==> Running $cmd\n";


输出是:

perl ./test2.perl ifbscdd root dcdiag "LEC=1 ; [ -d /tmp ] && LEC=9 ; export LEC ; return $LEC" 
 
Connected to ifbscdd 
 
    Authenticated as root 
 
    ## step 0 
 
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type, window_size, packet_size, ((void *)0) , 0) -> 0x1b89fc0 
 
0 
 
    ## step 2 
 
0 
 
    ## step 5 
 
Net::SSH2::Channel::read(size = 65, ext = 0) 
 
- read 65 bytes 
 
- read 65 total 
 
Linux ifbscdd 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 
 

 
libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type, window_size, packet_size, ((void *)0) , 0) -> 0x1ba6490 
 
0 
 
    ## step 6 
 
0 
 
    ## step 7 
 
0 
 
    ## step 8 
 
Net::SSH2::Channel::read(size = 65, ext = 0) 
 
- read 0 bytes 
 
- read 0 total 
 
0 
 
    ## step 9 
 
LEC=1 ; [ -d /tmp ] && LEC=9 ; export LEC ; return : 
 
Linux ifbscdd 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 
 

 
0 
 
    ## step 10 
 
-1LIBSSH2_ERROR_SOCKET_NONEFailed waiting for channel success 
 
    ## step 11 
 
Net::SSH2::Channel::DESTROY 
 
Net::SSH2::Channel::DESTROY 
 
Net::SSH2::DESTROY object 0x1aa6230

回答

0

只能调用shell每个通道对象一次。

此外,请注意shell方法不采取任何参数。为了执行,你必须把它写在通道上的命令:

$channel->write("$cmd\n"); 

或通常,相反,您的要求为您想运行,然后调用它的exec方法与命令每一个命令一个新的通道对象一个论点。总的来说,如果你正在试图做一个shell,那么这是一个糟糕的主意,而且很难做到。

在任何情况下,由于您在Linux环境中运行,而不是Net :: SSH2,这是相当原始和错误的,您可能喜欢使用Net::OpenSSH