Ssh Run a Remote Command: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 5: Line 5:
=Overview=
=Overview=


The simplest way to run a command remotely over ssh is to specify the command at the end of the ssh command line. If the command to be executed remotely includes species, the whole remote command must be enclosed in double quotes, as following:


!!!Multiple Remote Commands in the Same SSH Invocation
<pre>
ssh some-user@example-host "ls -l"
</pre>


{{{
'''Important:''' to avoid local shell substitution inside the remote command, escape $:


    ssh ovidiu@192.168.1.1 "ls ~; echo 'blah'; ls /tmp"
<pre>
ssh some-user@example-host "\$(hostname)"
</pre>


}}}
will return the remote hostname.
 
=Multiple Remote Commands in the Same SSH Invocation=


!Important
<pre>
ssh some-user@example-host "ls ~; echo 'blah'; ls /tmp"
</pre>


If storing the ssh command in a local variable and executing the local variable, the double quotes around the multiple commands are not necessary. This works
'''Important:''': If storing the ssh command in a local variable and executing the local variable, the double quotes around the multiple commands are not necessary. This works:


{{{
<pre>
ssh_command="ssh ovidiu@192.168.1.1 ls ~; echo 'blah'; ls /tmp"
ssh_command="ssh some-user@example-host ls ~; echo 'blah'; ls /tmp"
${ssh_command}
${ssh_command}
}}}
</pre>
 
!Important 2
 
To avoid local shell substitution inside the remote command, escape $:
 
{{{
    ssh ovidiu@192.168.1.1 "\$(hostname)"
}}}
 
will return the remote hostname.


!!!Multiple Commands with sudo Over ssh
!!!Multiple Commands with sudo Over ssh


|[sudo#MultipleCommandsWithSudoOverSsh]
|[sudo#MultipleCommandsWithSudoOverSsh]

Revision as of 20:46, 14 October 2016

Internal

Overview

The simplest way to run a command remotely over ssh is to specify the command at the end of the ssh command line. If the command to be executed remotely includes species, the whole remote command must be enclosed in double quotes, as following:

ssh some-user@example-host "ls -l"

Important: to avoid local shell substitution inside the remote command, escape $:

ssh some-user@example-host "\$(hostname)"

will return the remote hostname.

Multiple Remote Commands in the Same SSH Invocation

ssh some-user@example-host "ls ~; echo 'blah'; ls /tmp"

Important:: If storing the ssh command in a local variable and executing the local variable, the double quotes around the multiple commands are not necessary. This works:

ssh_command="ssh some-user@example-host ls ~; echo 'blah'; ls /tmp"
${ssh_command}

!!!Multiple Commands with sudo Over ssh

|[sudo#MultipleCommandsWithSudoOverSsh]