7

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 4

11

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.

2
  • What does the -s stand for in this context? How is this different than Innovator's answer ?
    – blong
    Jul 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.
    – S0AndS0
    Oct 16, 2019 at 3:47
3

Use this command:

ssh user@host <<'my.sh'
#script to run on remote host
my.sh
1

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.

2
  • 1
    As clearly mentioned, I do not want to copy the file over. Aug 5, 2014 at 5:53
  • 1
    Sorry, where is that "clearly mentioned"?
    – MadHatter
    May 4, 2015 at 21:37
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .