PDA

View Full Version : ssh to multiple machines via c program?



purest
12-12-2006, 05:46 AM
hi there,
I need to be able to run a comand on many computers (1000ish) in order to get information from them. currently im using a tcsh script that runs an ssh command for each box, but that takes age's and stops if one of the machines is broken.

i'm trying to write a c program that forks of lots of child processes to complete the task quckly.
any ideas?

i cant get it to work with system calls, is there a way to do ssh from within a c program without a system call?

thanks for any help.

Salem
12-12-2006, 06:13 AM
Maybe


char cmdLine[1000];
sprintf(cmdLine,"ssh %s mycommand", target );
system( cmdLine );

You can use sprintf() to build up any complicated command line you want.

purest
12-12-2006, 06:21 AM
yes, this is how i tried to do it!

BUT: it does not work if you do it from forked child processes. (and if it is done sequentialy its too slow.)
i think the problem is due to having multiple child processes sending system comands (to the same shell?) at the same time.

Salem
12-12-2006, 06:55 AM
I'm thinking you need to post more of what you attempted.

purest
12-12-2006, 08:01 AM
file attached.

jim mcnamara
12-12-2006, 08:54 AM
I don't do tcsh, so you get to translate....


#!/bin/ksh
for node in $1 $2 $3 $4 $5 $6
do
ssh "$node" "command ..." &
done
wait

You also have to know how many children the system allows - don't exceed the value. Shell is probably preferable to C code from a maintenance point of view if you are in a big shop.

Salem
12-12-2006, 11:25 AM
Well the initial problem is that you're no better off than running them sequentially, with all those wait() calls in there.

Also, as jim points out, you need some mechanism for not creating a whole bunch of child processes.
For example, create 10 children quickly, then wait for the first one to finish before creating another one.

Jim's use of & is another alternative