silent ssh is setup from server A to server B (and some 500 other servers)
I have written a script on server A (shell and perl) that I want to execute via ssh on server B (and the other 500 servers).
Is this possible ? I am able to run commands using silent ssh but not sure how to run entire scripts.
4 Answers
If Server A is a Unix/Linux-based system, you can use:
ssh root@MachineB 'bash -s' < local_script.sh
You shouldn't have to copy the script to the remote server to run it.
-
What does the
-s
stand for in this context? How is this different than Innovator's answer ?– blongJul 30, 2015 at 14:08 -
@blong
man --pager='less -p " -s "' bash
states "... This option allows the positional parameters to be set when invoking an interactive shell." where as the other mentioned answer assumes that the shell and settings are known to the client.– S0AndS0Oct 16, 2019 at 3:47
Since silent ssh is already setup is already setup as you say, I would scp the file the file and execute it locally
i.e:
while read line
do
echo Trying to configure server [IP]: $line >> error.log
scp my-script.sh $line:/root/scripts/ &>> error.log
ssh root@$line 'cd /root/scripts && ./my-script.sh' &> error.log
echo Finished working with [IP]: $line >> error.log
done <client-ips.txt
Running the script on client site is less error prone than parsing it with < << operators.
Something similar to the script above should do most of the work for you (hopefully all). Also it will keep track of anything ( &> forwards error messages) that went wrong so you know which IP addresses you need to attend manually.
-
1
-
1
#!/bin/bash
# Source : http://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/
# runremote.sh
# usage: runremote.sh localscript interpreter remoteuser remotehost arg1 arg2 ...
# example: runremote.sh MySQL_makeUser.sh bash pi coins.ml database user
realscript=$1
interpreter=$2
user=$3
host=$4
shift 4
declare -a args
count=0
for arg in "$@"; do
args[count]=$(printf '%q' "$arg")
count=$((count+1))
done
ssh $user@$host "cat | ${interpreter} /dev/stdin" "${args[@]}" < "$realscript"
# Note: you may need to add options or hardcode keys and such into the above command; example of this commented bellow
# ssh -i <path/to/key> -p <port> $user@$host "cat | ${interpreter} /dev/stdin" "${args[@]}" < "$realscript"
Above is a script I found with a little searching about and modified a bit to show an example of usage and example of using keys for connections as the OP stated that it would be run on many other servers. This script also is coded such that you may pass arguments to the local script and specify the program that the server should use to recieve the script's commands; ie you may tell your server to use perl or python or java... and then give it that related script :-D The source I found the above script is hard coded into it's comments such that copy/past will still allow you years latter to find the original authors ;-)
Happy networking to you all.