1

我制作了连接到我的交换机的脚本,但问题是当查看它时只显示部分内容,其余部分显示为大小(--更多)。我该怎么做才能查看所有配置,谢谢

use Net::OpenSSH;
use warnings;
use Expect;

my $password = 'admin';
my $enable = '';
my $ip = '192.16.25.39';
my $username='user';
my $ssh = Net::OpenSSH->new("$username:$password\@$ip", timeout => 200) ;
$ssh->error and die "unable to connect to remote host: ". $ssh->error;

my $output = $ssh->capture({stdin_data => "enable\n"."admin%\n"."show vlan"."\n"});

if ($output) {print $output . ' ';}

my $line;
print "\n";

# closes the ssh connection
$ssh->close();

我已经用 Expect 模块尝试过这个:

use Net::OpenSSH;
if ($output) {
    print $output . ' ';
    my $expect = Expect->init($output);
    $expect->raw_pty(1);
    #$expect->debug(2);
    my $debug and $expect->log_stdout(1);
    while(<$pty>) {
        print "$. $_ "
    }
}

产生此错误:

无法在 /usr/local/share/perl5/Expect.pm 第 202 行 (#1) (F) 祝福非参考值 (F) 只能祝福硬引用。这就是 Perl 如何“强制”封装对象。参见 perlobj。用户代码中未捕获的异常:Can't bless non-reference value at /usr/local/share/perl5/Expect.pm line 202. at /usr/local/share/perl5/Expect.pm line 202. Expect::exp_init ("期待", "\x{d}\x{a}witch>enable\x{d}\x{a}密码:\x{d}\x{a}switch#show vlan\x{d} \x{a}\x{d}\x{a}VLA"...) 在 b.pl 第 19 行调用"

4

1 回答 1

3

这可能是解决您的问题的更好方法。有一个Net::Telnet::Cisco模块可以简化与远程路由器的大量交互。显然,您可以先建立一个加密的 SSH 连接,Net::OpenSSH然后使用该连接中的文件句柄来启动Net::Telnet::Cisco会话。

所以我认为这样的事情比尝试Net::OpenSSH直接使用更有希望:

use Net::OpenSSH;
use Net::Telnet::Cisco;

my $password = 'admin';
my $enable = '';
my $ip = '192.16.25.39';
my $username='user';
my $ssh = Net::OpenSSH->new("$username:$password\@$ip", timeout => 200) ;
my ($pty, $pid) = $ssh->open2pty({stderr_to_stdout => 1})
  or die "unable to start remote shell: " . $ssh->error;
my $cisco = Net::Telnet::Cisco->new(
              -fhopen => $pty,
              -telnetmode => 0,
              -cmd_remove_mode => 1,
              -output_record_separator => "\r");
my @vlan = $cisco->cmd("show vlan");

我不熟悉配置 Cisco 路由器的细节,所以你必须从这里开始,但在我看来,这是一条更容易获得所需内容的途径。

于 2015-03-10T16:37:41.193 回答