Most Linux systems require you to do SSH login to remote server in order to run commands. Often you may need to run same set of commands on your remote server on a regular basis. It can be tedious to manually login to SSH every time and then run the commands. It is better to simply write a bash script to run commands on remote server. In this article, we will learn how to do this. You can use these steps on all Linux systems.
Bash Script to Run Commands on Remote Server
We will use HERE document feature of shell that allows you to feed command list to interactive programs such as SSH terminal. Here is its syntax.
COMMAND <<LimitString ... ... ... LimitString
In the above code block, we specify the initial interactive command or program to run and also mention the LimitString. After that, whatever commands you enter, they will be executed within the interactive program you called, until you enter the LimitString. Once LimitString is encountered, the execution within interactive program stops and control is returned back to your shell terminal.
So you can easily use this code block as shown below to run commands on remote server. Replace remote_host with your remote host IP address or URL.
ssh remote_host << EOF #command 1 #command 2 ... EOF
In the above code, we run the command ‘ssh remote_host’ to login to remote server. We also mention EOF as LimitString. Then we enter the commands we want to execute remotely. In the end we mention EOF to terminate remote execution and return control back to our shell.
Here is an example to run ls and some other commands remotely.
ssh remote_host << EOF ls some_folder; ./someaction.sh 'some params' pwd ./some_other_action 'other params' EOF
While running the above code, if you get the following message
Pseudo-terminal will not be allocated because stdin is not a terminal.
then modify the first line of your HERE document as
ssh remote_host /bin/bash << EOF
If you want to variable substitution you can enclose them within quotes but make sure that you don’t mention quotes in your LimitString.
NAME="abc" ssh remote_host /bin/bash << EOF touch "/tmp/${NAME}" EOF
In this case, the shell variable $NAME will be passed to your remote server and used in executing the remote command.
You can even create a cronjob to run the above shell script regularly.
For example, let us say you have saved the above HERE code block in a shell script at /home/ubuntu/test.sh. Make it executable with the following command.
$ sudo chmod +x /home/ubuntu/test.sh
Next, you can open crontab to create cronjob.
$ crontab -e
Add the following line to run the shell script every day at 10.a.m.
0 10 * * * /home/ubuntu/test.sh >/dev/null 2>&1
In this article, we have learnt how to execute commands on remote server. You can customize them as per your requirement.
Of course, if you just want to run a single command or a few commands, then you can directly include them in your SSH connection call.
$ ssh <username@IP_Address-or-Doman_name> <Command-or-Script>
For example here we are executing ‘uname -a’ command remotely.
$ ssh test@54.43.32.21 uname -a OR $ ssh test@54.43.32.21 "uname -a"
If you want to run multiple commands, you can combine them using && operator, or semicolon (;) operator.
$ ssh test@54.43.32.21 "uname -r && lsb_release -a" OR $ ssh test@54.43.32.21 "uname -r ; lsb_release -a"
But using HERE document is the cleanest way to run multiple remote commands since it leads to shorter code and also supports variable substitution.
Also read:
How to Add Image to Text in Python
How to Find & Copy Files in Linux
How to Rename Downloaded File in Wget
Wget Limit Download Speed Rate
How to Backup & Restore PostgreSQL database
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.