in reply to Unable to run command on Cisco switch

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 my.unix.box 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.

Replies are listed 'Best First'.
Re^2: Unable to run command on Cisco switch
by 1wax (Novice) on Sep 01, 2009 at 07:09 UTC
    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.
        Thanks for that. I used plink.
        The -m option did not work, so I redirected STDIN and it all works fine.
        The gotcha I discovered was that cisco switch was being parsed \r\n as newline. So that made going into enable mode a bit tricky. But that all sorted now by sending my command and a \r.
        Thanks.