nanomonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi is it possible to execute multiple commands over one, persistent SSH Terminal Session using Perl and the ssh binary?

NB: I cannot use Perl SSH libraries, any of them.

I am scripting a remote server and I need to execute a few commands, I just want to open an SSH Session, execute multiple remote commands and grab the output like this from within Perl:

ssh user@host command1 -->output command2 -->output .. and so on exit

instead of

ssh user@host command1-->output ssh user@host command2-->output .. and so on

Each of the latter has connect/auth overhead I want to avoid.

I have searched alot but really don't know how to define what I am looking for, I am sure perl's excellent I/O can do it, just not sure how, as I usually just ue backticks becuase the scripts tend to be local, but this one requires a remote ssh terminal session.

Thanks, nano.

Replies are listed 'Best First'.
Re: Persistent SSH Terminal from Perl?
by mikelieman (Friar) on Sep 26, 2008 at 17:32 UTC
    Perhaps Net-SSH-Expect ?
    DESCRIPTION This module is a wrapper to the ssh executable that is available in your system's $PATH. Use this module to execute commands on the remote SSH server. It authenticates with the user and password you passed in the constructor's attributes user and password. Once an ssh connection was started using the connect() method it will remain open until you call the close() method. This allows you execute as many commands as you want with the exec() method using only one connection. This is a better approach over other ssh wrapper implementations, i.e: Net::SCP, Net::SSH and Net::SCP::Expect, that start a new ssh connection each time a remote command is issued or a file is transfered.
Re: Persistent SSH Terminal from Perl?
by Illuminatus (Curate) on Sep 26, 2008 at 19:19 UTC
    I assume by 'libraries', you really mean modules, right? Why not? Is there some reason why you cannot simply use
    ssh user@host "command1;command2"
    If you want to use perl and you can setup ssh keys so you don't have to enter a password, you could use
    open (CHK,"| ssh user@host > /tmp/foo 2>&1") || die "failed"; print CHK "command1"; print CHK "command2"; close CHK; # process /tmp/foo here
Re: Persistent SSH Terminal from Perl?
by nanomonk (Initiate) on Sep 27, 2008 at 08:13 UTC
    Welcome to the brick wall

    Have looked into IO::React, no go (no Pty support on win). Now it looks like IPC::Run may help, but the documentation is very complicated, if anyone wishes to help me just start an ssh session (ssh -i key user@host), run two commands and get their output that would be great.

    Let's assume the two commands are:
    echo hello
    echo goodbye

      Has there been a solution to this problem? In particular running multiple commands to the remote machine over an SSH connection. I can do one command in a .pl script using:
      system('ssh user@host command');
Re: Persistent SSH Terminal from Perl?
by nanomonk (Initiate) on Sep 27, 2008 at 06:28 UTC
    OK SO compiling doesn't work for Expect (required by Net::SSH::Expect) becuase ultimatley: 'You cannot use IO::Tty with regular Perl on Windows because Windows doesn't use pseudo-terminals.' (from http://tinyurl.com/52j9nf), there is a pretty much unknown linker error which probably comes down to the OS symbols or something, no go there

    So it looks like I need to use Illuminatus', or some other 'unwrapped' method ("Illuminatus are you there?"), IF it can give output before closing the session, which I hope it can! Such a shame, I would love to be able to do this from win32 perl, it would make my life so much easier, it can't be that hard just to run ssh and send commands and get stdout & stderr back wihtout using modules can it? perl was made as a 'glue' tool in the first place..

      been looking at open2 to write and read to ssh.exe at the same time, but the problem theres no way to tell when the last command's output ended, so you can issue a command, then wait forever to get it's output, which is stupid. I think it's probably eaiser just to approach this job a different way, as Perl & SSH multiple commands on activeperl is just not happening. Thanks for help anyway.
Re: Persistent SSH Terminal from Perl?
by Anonymous Monk on Sep 27, 2008 at 05:58 UTC
    Thanks for replies, both enlightening.

    Client is ActivePerl on w32 1. I cannot get Net::SSH::Perl or Net::SSH::W32Perl to work, long story, too much hassle, no support.

    Net::SSH::Expect looks good, although I need to compile some of it's prerequisites which won't be easy, which I am doing now, so I will try that and get back.

    Secondarily, Illuminatus (Monk) I do not know the entire set of commands I want to execute in the beginning, some depend on output of others.

    So, in your second example, how do I get the output from one of the commands before closing the session?

    If that works, it will be the simplest & probably used method.

    Thanks.