Ssh Run a Remote Command: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=


* [[Ssh#Client_Operations|ssh Client]]
* [[OpenSSH Operations#Client_Operations|OpenSSH Client Operations]]


=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 contains spaces, the whole remote command must be enclosed in double quotes, as following:


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


{{{
'''Important:''' to avoid local shell substitution inside the remote command, escape special shell characters such as $ or *:


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


}}}
will return the remote hostname.


!Important
=Environment=


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
Executing a command remotely with ssh implies sourcing of .bashrc, but '''not''' of .bash_profile. If configuration from .bash_profile is needed, it has to be sourced explicitly:


{{{
ssh ". .bash_profile; ..."
ssh_command="ssh ovidiu@192.168.1.1 ls ~; echo 'blah'; ls /tmp"
${ssh_command}
}}}


!Important 2
=Multiple Remote Commands in the Same SSH Invocation=


To avoid local shell substitution inside the remote command, escape $:
<pre>
ssh some-user@example-host "ls ~; echo 'blah'; ls /tmp"
</pre>


{{{
'''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 ovidiu@192.168.1.1 "\$(hostname)"
}}}


will return the remote hostname.
<pre>
ssh_command="ssh some-user@example-host ls ~; echo 'blah'; ls /tmp"
${ssh_command}
</pre>


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


|[sudo#MultipleCommandsWithSudoOverSsh]
<font color=red>TODO https://home.feodorov.com:9443/wiki/Wiki.jsp?page=Sudo#section-Sudo-MultipleCommandsWithSudoOverSsh</font>

Latest revision as of 14:01, 13 July 2017

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 contains spaces, 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 special shell characters such as $ or *:

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

will return the remote hostname.

Environment

Executing a command remotely with ssh implies sourcing of .bashrc, but not of .bash_profile. If configuration from .bash_profile is needed, it has to be sourced explicitly:

ssh ". .bash_profile; ..."

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

TODO https://home.feodorov.com:9443/wiki/Wiki.jsp?page=Sudo#section-Sudo-MultipleCommandsWithSudoOverSsh