Perl Net :: SSH ::期望不打印出所有预期的输出

问题描述:

我在perl中使用期望从我的路由器获取接口信息。当我在远程路由器上运行命令时,应该在那里丢失大约10-15行。不知道为什么它停止,有什么想法?Perl Net :: SSH ::期望不打印出所有预期的输出

#!/usr/bin/perl -w 

#use strict; 
use warnings; 
use Net::SSH::Expect; 

my $ssh = Net::SSH::Expect->new (
     host => "10.10.10.10", 
     user => 'user', 
     password => 'pass' 
     ); 
my $login_output = $ssh->login(); 
     if ($login_output !~ /router#/) { 
      die "Login has failed. Login output was $login_output"; 
     } 
#$ssh->run_ssh() or die "SSH process couldn't start: $!"; 
$ssh->send("show int g2/1"); 
my $line; 

while (defined ($line = $ssh->read_line())) { 
     print $line."\n"; 
} 
+3

是否有可能'read_line()'是即使路由器将是第二次以后发送的详细信息,缺省为1秒后超时? – geoffspear

+0

我不这么认为,它会打印出线条然后停下来停下来。我尝试添加一个超时变量,但没有骰子... – user2859077

+0

1.是否捕获到STDERR?如果您直接ssh到路由器并运行'show int g2/1>/dev/null'。还有什么东西仍然印在屏幕上? 2.输出是否在不同的线路上使用不同的端子? –

我怀疑,因为你正在处理一个路由器,要启用raw_pty => 1,如Net::SSH::Expect文档建议。另外,使用 - > exec调用代替 - > send + read_line可能更容易。

要进一步调试,请将log_stdout传递给Net :: SSH :: Expect构造函数,并查看是否可以检测到发生任何错误。你为什么评论'严格使用'?始终'严格使用'和'使用警告'

Net :: SSH :: Expect不可靠。使用其他模块为Net::OpenSSHNet::SSH2Net::SSH::Any或者只是Expect

use Net::OpenSSH; 
my $ssh = Net::OpenSSH->new("10.10.10.10", 
          user => 'user', 
          password => 'pass', 
          timeout => 60); 

my $output = $ssh->capture('show int g2/1'); 

# or for some non-conforming SSH server implementations rather 
# common in network equipment you will have to do... 
my $output = $ssh->capture({stdin_data => "show int g2/1\n"}); 

$ssh->error and die "unable to run remote command: " . $ssh->error; 
+0

谢谢salva这是完美的! – user2859077