Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 6 down vote accepted

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.

share|improve this answer

Use this command:

ssh user@host <<'my.sh'
#script to run on remote host
my.sh
share|improve this answer

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.

share|improve this answer
    
As clearly mentioned, I do not want to copy the file over. –  user1074593 Aug 5 '14 at 5:53
    
Sorry, where is that "clearly mentioned"? –  MadHatter May 4 at 21:37
#!/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.

share|improve this answer

You can execute remote commands over ssh like

ssh user@serverA /path/to/your/script

share|improve this answer
    
This will execute the script on serverA ! Its not what I want. –  user1074593 Aug 5 '14 at 5:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.