in reply to Perl with Net::SSH2, vshell and psexec

Using Net::SSH2 correctly is quite hard and may involve digging into Net::SSH2 and libssh2 source code in order to discover how things really work and why your theoretical correct program doesn't.

If you are on a Linux/Unix machine, I advice you to use Net::OpenSSH instead.

  • Comment on Re: Perl with Net::SSH2, vshell and psexec

Replies are listed 'Best First'.
Re^2: Perl with Net::SSH2, vshell and psexec
by mmenza (Initiate) on Feb 10, 2014 at 12:42 UTC

    Using Net::SSH2 correctly is quite hard and may involve digging into Net::SSH2 and libssh2 source code in order to discover how things really work and why your theoretical correct program doesn't.

    I second this! I use the perl Expect module to control the shell ssh command.

    I do a lot of cisco ios network automation using perl, and have published an open source suite of modules to handle connectivity, logging, testing, etc. They are posted at menzascripting.com. The Mnet::Expect module has my code for controlling ssh sessions (with authentication, prompt detection, telnet fallback, etc). Feel free to view or use that code. There a lot of quirk-mitigation built into it.

      I have used net::telnet and ssh::expect with great results with network equipment (hp, cisco, brocade, enterasys). For all my network needs (sh arp, mac, route, config backups). Not any problems with equipment.

      This psexec with hidden forking is a new monster altogether.

Re^2: Perl with Net::SSH2, vshell and psexec
by t_rex_joe (Sexton) on Feb 10, 2014 at 14:56 UTC
    Thanks for the reply..

    I have used net::openssh on another variant of these scripts.. Again no such luck. Currently I have 15 variants using: Net::SSH::Expect, Net::SSH2, Net::SSH::Perl, Net::OpenSSH. Thinking one of these would work.

    I still get a "Caption" output like the one stated in the original post using Net::OpenSSH. It seems somehow when the process is forked the SSH client (perl) loses this channel/output.

      What do you get when you run the command through ssh without a tty?
      ssh $WINDOWS_HOST 'c:\psexec.exe /accepteula c:\psexec.exe /accepteula + \\localhost wmic OS get Caption'

      psexec may be expecting a GUI environment, event if it doesn't seem to use it.

      The issue may also be related to quoting. Windows SSH servers use different approaches to launch commands. Some run them through cmd.exe, others just call the function CreateProcess from the Win32 API, etc.

        Salva,

        Thanks for the response..

        Here's the output:

        PsExec v1.98 - Execute processes remotely Copyright (C) 2001-2010 Mark Russinovich Sysinternals - www.sysinternals.com c:\psexec.exe exited with error code 0.
        The caption portion does not appear..