views:

216

answers:

4

I have roughly 12 computers that each have the same script on them. This script merely pings all the other machines, and prints out whether the machine is "reachable" or "unreachable". However, it is inefficient to login to each machine manually using ssh to execute this script.

Suppose I'm logged into node 1. Is there any way to for me to login to node 2-12 automatically using SSH, execute the ping script, pipe the results to a file, logout and proceed to the next machine? Some kind of bash shell script?

I'm afraid I'm at a loss here since I haven't had experience with shell-scripting before.

+1  A: 

Since the script is on the other machines, you can just have ssh run the command for you there:

ssh $hostname my_script >> results_file

When you specify a command like that, it's executed instead of the login shell.

I'll leave it up to you to figure out how to loop over hostnames!

Jefromi
Since the OP lacks shell-scripting experience, I'll offer a hint: `hosts="host1 host2 host3"; for host in $hosts; do ssh $host ...; done`
Dennis Williamson
A: 

One trick you'll need to use is setting up pre-authorized keys for each host. Then you can run a script on one host, running something like 'ssh hostname command > log.hostname'

Andrew B
A: 

This script might be what you are looking for: It allows you to execute one command (which can be your script) on multiple remote machines via ssh. It's a simple script with bash source available, so you should be able to customize it to your needs:

http://www.heinzi.at/projects/upgradebest.sh/

Heinzi
A: 

Yes you can
You need actually 2 small scripts as following:
remote_ssh.sh ( which takes as first argument the name of the machine and the rest of the arguments are your script that you want to execute with his own arguments)
Example : remote_ssh.sh node5 "echo hello world"

remote_ssh.sh as following:

#!/bin/bash
ALL_ARG=$@
FST_ARG=$1
REST_ARG=${ALL_ARG##$FST_ARG}
echo "Executing REMOTE COMMAND ON $FST_ARG"
/usr/bin/ssh $FST_ARG bash execute_ssh_command.sh $FST_ARG pwd $REST_ARG

execute_ssh_command.sh as following :

#!/bin/bash
ALL_ARG=$@
FST_ARG=$1
DIR_ARG=$2
REM_ARG="$1 $2"
REST_ARG=${ALL_ARG##$REM_ARG}

cd $DIR_ARG
$REST_ARG

of course you have to get this 2 scripts in your path of all your nodes ( maybe ~/bin/ )

Hope that it's helpful