How To Run Multiple SSH Command On Remote Machine And Exit Safely

How To Run Multiple SSH Command On Remote Machine And Exit Safely

last updated in Categories , , , , , , , , ,


I have a backup sync program on local server. I have an ssh password less login set up, and I can run commands on an external server in bash script doing:
ssh root@server2 "sync; sync; /sbin/shutdown -h now"
How do I run multiple commands in bash on a remote Unix or Linux server? What is the best way to SSH in and run various unix commands in a bash shell?

[donotprint][/donotprint]There are various ways to run multiple commands on a remote Unix server. This page show the easiest way to ssh and run multiple commands in using bash shell. The syntax is as follows.




Simple bash syntax to run multiple commands on remote machine

Simply run command2 if command1 successful on a remote host called foo:
$ ssh bar@foo "command1 && command2"
For example, run uptime and date commands on a box named ‘centos7’ as ‘vivek’ user::
ssh vivek@centos7 "uptime && date"
Sample outputs:

 18:36:17 up 1 day,  8:14,  0 users,  load average: 1.02, 1.06, 1.05
Fri Jun  7 18:36:17 IST 2019

How To Run Multiple SSH Command

Run date and hostname commands:
$ ssh user@host "date && hostname"
You can run sudo command as follows on a remote box called
$ ssh -t "sudo /sbin/shutdown -h now"
And, finally:
$ ssh "sync && sync && /sbin/shutdown -h now"
How To Run Multiple SSH Command On Remote Linux or Unix Machine

Bash here document syntax to run multiple SSH command

The here document syntax tells the shell to read input from the current source (HERE) until a line containing only word (HERE) is seen:

ssh server1 << HERE

Here is another example to run a multiple commands on a remote system:

ssh << EOF
 cat /etc/resolv.conf

With sudo command type:

ssh -t << EOF
 sudo /sbin/shutdown -h 0

Pipe script to a remote server

Finally, create a script called “remote-box-commands.bash” as follows on your local server:

cat /etc/resolv.conf

Run it as follows on a remote server called nas02nixcrafthomeserver:
$ cat remote-box-commands.bash | ssh user@nas02nixcrafthomeserver
cat remote-box-commands.bash | ssh -t vivek@nas02nixcrafthomeserver

How to ssh to multiple servers and run commands

One can use simple bash for loop as follows. In this example, run uptime command on three Linux server named box1, box2, and box3:

for s in box1 box2 box3
   ssh vivek@${s} uptime

Running local bash script on remote system

Create a shell script as follows:

# Name: 
# Purpose: Run multiple commands on a remote box 
# ----------------------------------------------------

Now run it as follows on host named
ssh 'bash -s' < /path/to/
OR run it on four servers:

for s in server{1..4}
   ssh vivek@${s} 'bash -s' < /home/vivek/bin/

Please note that the -t option will get rid of "Sorry, you must have a tty to run sudo/insert your-command-here" error message. You can also use pssh (parallel ssh) for executing ssh in parallel on a number of Linux/Unix/BSD servers. For more info see your ssh and bash command man page:
$ man ssh
$ man bash
$ man sudo

And there you have it, multiple bash shell command run using nothing but ssh client/sever model. A better option is to use the ansible tool.

0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Linux Screen
Viewed 866 times since Sat, Jun 2, 2018
Linux File Systems (mkfs, mount, fstab) ext4
Viewed 946 times since Sat, Jun 2, 2018
RHEL: Multipathing basics
Viewed 1419 times since Sat, Jun 2, 2018
HOWTO: Use SSL/port 465 in smarthost stunnel
Viewed 644 times since Fri, Sep 28, 2018
Monitoring bezpieczeństwa Linux: integracja auditd + OSSEC cz. I
Viewed 829 times since Fri, Apr 5, 2019
Set Up SSH Tunneling on a Linux / Unix / BSD Server To Bypass NAT
Viewed 210 times since Fri, May 15, 2020
Linux – Securing your important files with XFS extendend attributes
Viewed 585 times since Wed, Jul 25, 2018
RHEL: Extending the maximum inode count on a ext2/ext3/ext4 filesystem
Viewed 1189 times since Sun, May 27, 2018
linux manual tools
Viewed 648 times since Fri, Sep 28, 2018
logrotate Understanding logrotate utility
Viewed 359 times since Sun, Jan 12, 2020