Thread: ssh to multiple machines via c program?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    ssh to multiple machines via c program?

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe
    Code:
    char cmdLine[1000];
    sprintf(cmdLine,"ssh %s mycommand", target );
    system( cmdLine );
    You can use sprintf() to build up any complicated command line you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm thinking you need to post more of what you attempted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    file attached.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    I don't do tcsh, so you get to translate....
    Code:
    #!/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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. SSH program
    By afreedboy in forum Tech Board
    Replies: 2
    Last Post: 03-18-2005, 10:47 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. Replies: 18
    Last Post: 12-05-2003, 12:06 PM