Ssh Run a Remote Command: Difference between revisions
Jump to navigation
Jump to search
Line 9: | Line 9: | ||
ssh some-user@example-host "ls -l" | ssh some-user@example-host "ls -l" | ||
{{Warn| '''Important:''' to avoid local shell substitution inside the remote command, escape special shell characters such as $ or *:}} | |||
ssh some-user@example-host "\$(hostname)" | ssh some-user@example-host "\$(hostname)" |
Revision as of 13:56, 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.
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