【问题标题】:Displaying a portion of the configuration (--More)显示部分配置(--更多)
【发布时间】:2015-05-12 00:33:03
【问题描述】:

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

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) 祝福非引用值。这就是 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 行调用"

【问题讨论】:

  • “--More--”输出听起来像是交换机本身响应“show vlan”命令而产生的。我想看看您是否可以发出命令,使开关在返回之前不等待键盘输入。如果无法做到这一点,使用Expect module with Net::OpenSSH 可能会有所帮助。
  • 我用过Expect,但是遇到了问题:(
  • 当我使用 Expect 时,我有这样的消息,例如输出“can't bless non-reference value at /usr/local/share/perl5/expect .pl line 202”任何解决方案好吗?跨度>
  • 我用expect试试这个:使用Net::OpenSSH;如果($输出){打印$输出。 ' ';我的 $expect = Expect->init($output); $expect->raw_pty(1); #$expect->debug(2);我的 $debug 和 $expect->log_stdout(1); while() { print "$.$_" }}
  • 这就是问题“不能在 /usr/local/share/perl5/Expect.pm 第 202 行 (#1) (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 第 202 行。Expect::exp_init("Expect", "\x{d}\x{a}witch>enable\x{d}\x{a}password:\x{d} \x{a}switch#show vlan\x{d}\x{a}\x{d}\x{a}VLA"...) 在 b.pl 第 19 行调用"

标签: perl cisco-ios expect.pm


【解决方案1】:

这可能是解决您的问题的更好方法。有一个Net::Telnet::Cisco 模块可以简化很多与远程路由器的交互。显然,您可以先使用Net::OpenSSH 建立一个加密的SSH 连接,然后使用该连接中的文件句柄来启动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 路由器的细节,所以你必须从这里开始,但在我看来,这是一条更容易获得所需内容的途径。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2014-01-19
    • 2014-10-29
    • 1970-01-01
    相关资源
    最近更新 更多