Bash scripts I use often

Run the same command remotely using SSH on multiple machines

Here is how it works:-

$> on='vinay@serverA vinay@serverB' run='free -g' ./now
running free -g on vinay@serverA
             total       used       free     shared    buffers     cached
Mem:           125         93         32          0          0         13
-/+ buffers/cache:         79         46
Swap:            0          0          0

running free -g on vinay@serverB
             total       used       free     shared    buffers     cached
Mem:           125         84         41          0          0          0
-/+ buffers/cache:         84         41
Swap:            0          0          0

Here is the script now that does that. Simple huh!!

#!/bin/bash
for server in $on
do 
 echo "running $run on $server"
 ssh $server $run
done

looping over an range from file

ssh doesn’t pick the environment set by .bash_profile & therefore one has to explicitly set it for the ssh command executed.

#!/bin/bash
for server in `cat activeServers.txt` 
do
  ssh vinay@$server '. ~/.bash_profile;nohup ./server/bigmemory-max-4.0.5/server/bin/start-tc-server.sh &'
done
Advertisements