1

我在 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";
}
4

2 回答 2

1

Net::SSH::Expect 不可靠。使用其他模块作为Net::OpenSSH , Net::SSH2 , Net::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;
于 2013-10-09T07:40:12.700 回答
0

我怀疑由于您正在处理路由器,因此您希望像Net::SSH::Expect文档建议的那样启用 raw_pty => 1 。此外,使用 ->exec 调用而不是 ->send + read_line 可能更容易。

为了进一步调试,将 log_stdout 传递给 Net::SSH::Expect 构造函数,看看是否能检测到任何异常情况。你为什么注释掉'use strict'?始终“使用严格”和“使用警告”

于 2013-10-09T00:51:37.133 回答