Admin-Ahead Community

Linux => General Linux => Topic started by: jibinw on July 21, 2018, 02:36:31 pm

Title: Execute commands remotely using SSH
Post by: jibinw on July 21, 2018, 02:36:31 pm
In this article I describe a few ways to execute commands on a remote host using SSH.

Single-line command

Executing a single command:

Code: [Select]
ssh $HOST ls
Executing several commands, inlined, separated with ;

Code: [Select]
ssh $HOST ls; pwd; cat /path/to/remote/file
Executing a command with sudo
Code: [Select]
ssh $HOST sudo ls /root
sudo: no tty present and no askpass program specified

sudo requires interactive shell, it can be enabled with -t parameter.

Code: [Select]
ssh -t $HOST sudo ls /root
[sudo] password for zaiste:

Simple multi-line command

Code: [Select]
VAR1="Variable 1"
ssh $HOST '
ls
pwd
if true; then
    echo "True"
    echo $VAR1      # <-- it won't work
else
    echo "False"
fi
'

Shell variables won't be expanded with this method.

Multi-line command with variables expansion

In order to make variables expansion work, use bash -c.

Code: [Select]
VAR1="Variable 1"
ssh $HOST bash -c "'
ls
pwd
if true; then
    echo $VAR1
else
    echo "False"
fi
'"

Multi-line command from local script

A local script can be executed against remote machine with a simple stdin redirection.

Code: [Select]
cat script.sh
ls
pwd
hostname

Code: [Select]
ssh $HOST < script.sh
Multi-line command using Heredoc

Using heredoc is probably the most convenient way to execute multi-line commands on a remote machine. Also, variables expansion works out-of-the-box.

Code: [Select]
VAR1="boo"
ssh -T $HOST << EOSSH
ls
pwd
if true; then
  echo $VAR1
else
  echo "False"
fi
EOSSH



If you need to assign variables within the heredoc block, put the opening heredoc in single quotes.

Code: [Select]
ssh -T $HOST <<'EOSSH'
VAR1=`pwd`
echo $VAR1

VAR2=$(uname -a)
echo $VAR2

EOSSH

The following warning message

Pseudo-terminal will not be allocated because stdin is not a terminal.

can be disabled by adding -T parameter to ssh execution.