[plug] multiple commands in a single ssh connection

Daniel Pittman daniel at rimspace.net
Fri Oct 10 12:27:45 WST 2008


"Adam Hewitt" <ahewitt at theozhewitts.com> writes:

> I have a requirement to write a script that is able to send multiple
> lines of commands via a single ssh connection. I have tried a whole
> bunch of things but nothing seems to be working and google isn't being
> friendly.
>
> Is this actually possible and if so can someone give me a pointer?

As pointed out elsewhere, ';' is your friend, as is writing a shell
script that can be run as a single command on the remote host.

You could just try sending the command with newlines embedded as the
single argument to ssh; this works fine for me:

    ssh example.com 'echo one
    echo two'

That runs as two commands.


You might, perhaps, find that Expect or the port of it to your preferred
scripting language, useful for driving the interactive ssh command and
responding to the remote commands.

Alternately, you will find that there are SSH libraries available for
most common languages, which will allow you to establish the connection
and send commands in series.


Finally, you could approach this by outlining what you are trying to
do -- the problem you face, not the issue with the solution you think
you have found.

In my experience "I need to send multiple commands through a single SSH
session" is usually a mistake in my probably analysis -- every time I
have thought that there has been a better way...

So, if you tell us what the problem is we can, perhaps, suggest a more
effective solution for you.

Regards,
        Daniel



More information about the plug mailing list