Assume you have a complex bash script named 'run_q.sh' (containing good number of loops, Awk statements, multiple lines of code etc) that you need to run against one or more remote servers via SSH. One way would be to copy (i.e. scp) the script to the remote server(s) and then do something like this: e.g.
$ scp -2 run_q.sh firstname.lastname@example.org:/tmp/. $ ssh -2 email@example.com sh /tmp/run_q.shIf its multiple machines, then you can use bash for loop like this:
$ for ip in 192.168.32.8 192.168.32.9 192.168.32.3 ; do echo "Executing on IP: $ip" ; ssh -2 root@$ip 'sh /tmp/run_q.sh' ; doneNow assume you do not want to copy your script to the remote machine(s) and want to execute from your local machine without copying it. Here's a way:
$ cat run_q.sh | ssh -2 firstname.lastname@example.org /bin/shAnd if its to be executed on multiple machines, you can use a for loop like this:
$ for ip in 192.168.32.8 192.168.32.9 192.168.32.3 ; do echo "Executing on IP: $ip" ; cat run_q.sh | ssh -2 root@$ip /bin/sh ; doneI am sure you liked this trick ! Feel free to comment below. Thanks.