Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Unable to run command on Cisco switch

by 1wax (Novice)
on Aug 31, 2009 at 12:07 UTC ( #792350=perlquestion: print w/replies, xml ) Need Help??
1wax has asked for the wisdom of the Perl Monks concerning the following question:

I am having a problem when i run the following perl code to try and get some info from a cisco switch.
The screen hangs at channel 0: open confirm rwindow 1024 rmax 4096

use Net::SSH::W32Perl ;
use diagnostics;

$ENV{HOME} = "C:/Temp";
$scon = Net::SSH::W32Perl->new ("x.x.x.x", debug => 2);

my ($output,$errors,$exit) = $scon->cmd("show run | i snmp" . "\n");
print $output;

Debug extract:
< SNIP >
Authentication methods that can continue: password.
Next method to try is password.
Trying password authentication.
channel 0: new client-session
Requesting channel_open for channel 0.
Entering interactive session.
Sending command: show run | i snmp

Requesting service exec on channel 0.
channel 0: open confirm rwindow 1024 rmax 4096
< SNIP >
I have searched for all possible reasons including amending the package to only launch one channel to the switch.
It still hangs at the output and does not respond.

Replies are listed 'Best First'.
Re: Unable to run command on Cisco switch
by salva (Abbot) on Aug 31, 2009 at 12:53 UTC
    The SSH protocol allows for two modes of operation:

    The first one is the interactive mode, designed to "talk" with humans, that just launches a shell and attaches it to the SSH channel, that is expected to be attached to the user console in the computer running the SSH client.

    The second mode, allows to run independent commands. When it is used, the SSH server runs the given command attaching its IO streams to the SSH channel. It is what you get, for instance, when you run

    $ ssh cat /etc/passwd
    and it is the mode used by Net::SSH::Perl for its cmd method.

    Unfortunately, some servers (as probably the one you are using) do not support this mode and unconditional launch a new shell for every new channel created.

    To work-around that, you have to talk directly to the shell, and for that Expect is your best option, or even Net::SSH::Expect that is a wrapper around Expect specifically designed to talk to SSH servers.

    ... but Expect does not work on Windows, well, it doesn't work with ActiveState Perl (or AFAIK, with Strawberry Perl), but it works with Cygwin Perl.

    Probably, Net::SSH::Perl (or Net::SSH2) have low level methods allowing to handle an interactive conversation with a channel, but it's not going to be as easy as with Expect!

    update: and BTW, Net::SSH::W32Perl was a hack to make old versions of Net::SSH::Perl work under Windows. AFAIK, its logic was incorporated into the main module and so it shouldn't be used anymore.

      Many thanks for the prompt responses.
      I have been down the road of trying to use Net::SSH::Expect but as you've rightly said, it would not work on a windows machine.
      The other modules I looked at require some sort of pty, which windows does not support. Net::SSH2 does not work due to the requirement to have libssh2 on the machine.
      I will try using cygwin again. Had problems with the installation. Will report back if i could get cygwin working. Otherwise the quest continues
        Another option would be to use plink (the command line ssh client that comes with PuTTY). AFAIK, it doesn't try to emulate TTYs in Windows so it would be easier to automate than OpenSSH ssh client.
Re: Unable to run command on Cisco switch
by raisputin (Scribe) on Aug 31, 2009 at 18:43 UTC
    I have a ot of small programs that gather data from Cisco and Cisco-like switches. Net::SSH::Expect is what I have to use to get this to work.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://792350]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2018-06-22 23:06 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.