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

时间:2013-10-08 14:47:40

标签: perl

我在perl中使用expect从路由器获取接口信息。当我在远程路由器上运行命令时,它缺少大约10-15行应该在那里。不确定为什么停止,任何想法?

#!/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";
}

2 个答案:

答案 0 :(得分:1)

Net :: SSH :: Expect不可靠。将其他模块用作Net::OpenSSHNet::SSH2Net::SSH::AnyExpect

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;

答案 1 :(得分:0)

我怀疑你正在处理路由器,你想启用raw_pty => 1,如Net::SSH::Expect文档所示。此外,您可能更容易使用 - > exec调用而不是 - > send + read_line。

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