07 Jun2016
How to run a bash script remotely in Linux
If you manage multiple Linux servers, you will soon find out that being able to run a bash script remotely will save you alot of time due to the fact that you no longer have to copy the script to each server before running it.
In this quick tutorial, I will show you how simple it is to do this. Say your script is called myscript.sh and stored on your local machine, you will run it as follows:
ssh root@your_server1 "bash -s" < myscript.sh
Simple as that. Now just add this to a for loop to run the script on all your servers, for example:
for test in your_server1 your_server2 your_server3 ; do echo $test ; ssh root@$test "bash -s" < myscript.sh ; done
If you have your aliases setup in the ssh config file, it is even easier.
for test in server{1..3} ; do echo $test ; ssh root@$test "bash -s" < myscript.sh ; done
Always be VERY careful when running a script across multiple servers for obvious reasons.
Hopefully this quick tutorial will make your life a bit easier.