1

I have got this error when i try to connect to my switch !

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();

I have tried this with the Expect module:

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 "$. $_ "
    }
}

which produces this error:

Can't bless non-reference value at /usr/local/share/perl5/Expect.pm line 202 (#1) (F) Only hard references may be blessed. This is how Perl "enforces" encapsulation of objects. See perlobj. Uncaught exception from user code: 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("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"...) called at b.pl line 19 "

ahmed
  • 19
  • 4
  • The "--More--" output sounds like it's being produced by the switch itself in response to the "show vlan" command. I would see if you can issue the command in such a way that the switch doesn't wait for keyboard input before returning. If that can't be done, using the [Expect module with Net::OpenSSH](http://search.cpan.org/~salva/Net-OpenSSH-0.62/lib/Net/OpenSSH.pm#Expect) may be helpful. – Tim Pierce Mar 10 '15 at 14:38
  • I used Expect, but I have encountered problem :( – ahmed Mar 10 '15 at 15:09
  • when i use Expect , i have this message like output "can't bless non-reference value at /usr/local/share/perl5/expect .pl line 202" any solution please ? – ahmed Mar 10 '15 at 15:16
  • i try this with 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 "$. $_ " }} – ahmed Mar 10 '15 at 15:21
  • this is the problem " Can't bless non-reference value at /usr/local/share/perl5/Expect.pm line 202 (#1) (F) Only hard references may be blessed. This is how Perl "enforces" encapsulation of objects. See perlobj. Uncaught exception from user code: 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("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"...) called at b.pl line 19 " – ahmed Mar 10 '15 at 15:24
  • i find it :) :) without expect :) – ahmed Mar 10 '15 at 16:43

1 Answers1

3

This might be a better approach to your problem. There is a Net::Telnet::Cisco module that simplifies a lot of the interaction with the remote router. Apparently you can first set up an encrypted SSH connection with Net::OpenSSH and then use the filehandle from that connection to start a Net::Telnet::Cisco session.

So I think something like this would be more promising than trying to use Net::OpenSSH directly:

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");

I am not familiar with the ins and outs of configuring Cisco routers, so you'll have to take it up from here, but this looks to me like a much easier route to get what you need.

Tim Pierce
  • 5,514
  • 1
  • 15
  • 31