In my previous article , we have discussed about how we can execute remote command over ssh in Linux/Unix. What if we want to execute multiple commands or shell script? In this article we will be discussing about how to run-execute multiple remote commands-shell scripts using ssh in Linux / UNIX.
Now you are very much aware about executing single remote command using ssh as below:
# ssh lradmin@172.31.7.221 df
Syntax to run-execute multiple remote commands-shell scripts using ssh
ssh USER@HOST 'command1';'command2';'command2';....;'commandN'
Here:
ssh –> is the protocol or command being used for making connection with remote server. USER –> is the username on remote server. HOST –> is the remote host on which you want to execute remote command. Command –> are the commands that you want to execute on remote host.
-
Simple Multiple remote commands using ssh
Lets say you want to have the output of df command and uptime command in one go, then follow below command:
[root@rhel1 ~]# ssh lradmin@172.31.7.221 df;uptime Filesystem 1K-blocks Used Available Use% Mounted on /dev/xvda2 10473452 1581544 8891908 16% / devtmpfs 489456 0 489456 0% /dev tmpfs 507736 0 507736 0% /dev/shm tmpfs 507736 12972 494764 3% /run tmpfs 507736 0 507736 0% /sys/fs/cgroup tmpfs 101548 0 101548 0% /run/user/1000 tmpfs 101548 0 101548 0% /run/user/0 00:25:16 up 16 min, 1 user, load average: 0.00, 0.01, 0.04 [root@rhel1 ~]#
-
Conditional Multiple remote commands using ssh
You can even execute conditional flow described in this article. For example you want to execute second command only if first command is successfully executed.
[root@rhel1 ~]# ssh lradmin@172.31.7.221 'mkdir /tmp/data && cd /tmp/data'
Here in this case second command “cd” is executed remotely only if mkdir command executed successfully. Please make sure you enclose your multiple commands in quotes ” ‘ “.
-
Remote sudo commands using ssh
Even you can execute remote commands using sudo facility available in Linux as below:
[root@rhel1 ~]# ssh -t manmohan@172.31.7.221 "sudo ls -al /root" [sudo] password for manmohan: total 44 dr-xr-x---. 4 root root 4096 Jun 12 12:08 . dr-xr-xr-x. 19 root root 4096 Jun 12 10:35 .. -rw-------. 1 root root 7548 Mar 1 2016 anaconda-ks.cfg -rw-------. 1 root root 2780 Jun 20 01:23 .bash_history -rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc -rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc -rw-------. 1 root root 62 Jun 12 10:35 .passwd-s3fs drwxr-----. 3 root root 18 Jun 12 10:21 .pki drwx------. 2 root root 28 Jun 12 10:11 .ssh -rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc Connection to 172.31.7.221 closed.
Note: Make sure you include “-t” option since sudo requires interactive shell , Otherwise you will get below message:
[root@rhel1 ~]# ssh manmohan@172.31.7.221 "sudo ls -al /root" sudo: sorry, you must have a tty to run sudo [root@rhel1 ~]#
-
Multiple remote commands using Here doc
Another awesome method for executing multiple remote commands is using “Here document”.In this we enclose our list of commands within a tag word like “HERE” in the below example. Basically it signifies start and stop of the commands.
Basic Syntax:
ssh USER@SERVER << HERE command1 command2 HERE
Basically in this type we enclose our commands withing two common words. In the above syntax we have used “HERE” for indicating start and stop of commands.
Example:
[root@rhel1 ~]# ssh -T lradmin@172.31.7.221 << HERE uptime free -m date HERE 01:14:47 up 1:05, 1 user, load average: 0.00, 0.01, 0.05 total used free shared buff/cache available Mem: 991 76 779 12 136 774 Swap: 0 0 0 Wed Jun 21 01:14:47 EDT 2017 [root@rhel1 ~]#
In above example we have used “-T” option to remove the “Pseudo-terminal will not be allocated because stdin is not a terminal.” message during execution.
Another example:
[root@rhel1 ~]# ssh -T lradmin@172.31.7.221 << MYCOMMANDS w df -h hostname MYCOMMANDS 01:17:38 up 1:08, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT ec2-user pts/1 43.242.225.117 00:23 12:34 0.12s 0.19s sshd: ec2-user [priv] Filesystem Size Used Avail Use% Mounted on /dev/xvda2 10G 1.6G 8.5G 16% / devtmpfs 478M 0 478M 0% /dev tmpfs 496M 0 496M 0% /dev/shm tmpfs 496M 13M 484M 3% /run tmpfs 496M 0 496M 0% /sys/fs/cgroup tmpfs 100M 0 100M 0% /run/user/1000 tmpfs 100M 0 100M 0% /run/user/0 ip-172-31-7-221.ap-south-1.compute.internal [root@rhel1 ~]#
-
Multiple remote commands using Bash Script over ssh
You can also run-execute multiple remote commands within a bash script. Here first need to write a local bash script as below:
[root@rhel1 ~]# cat myscript.sh free -m date hostname uptime [root@rhel1 ~]#
Now Execute this local bash shell script using below method on remote Linux server as below:
[root@rhel1 ~]# ssh lradmin@172.31.7.221 'bash -s' < myscript.sh total used free shared buff/cache available Mem: 991 75 779 12 136 774 Swap: 0 0 0 Wed Jun 21 01:25:38 EDT 2017 ip-172-31-7-221.ap-south-1.compute.internal 01:25:38 up 1:16, 1 user, load average: 0.00, 0.01, 0.05 [root@rhel1 ~]#
In this way we can run-execute multiple remote commands-shell scripts using ssh on Linux/Unix server.
David Mulcihy says
Your first example: ssh lradmin@172.31.7.221 df;uptime
has an issue. The “;” ends the ssh command. Thus the uptime command is executed on the local machine, not the remote machine.