Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!barnett From: barnett@grymoire.crd.ge.com (Bruce Barnett) Newsgroups: comp.lang.perl Subject: muxrsh.pl: parallel rsh commands Message-ID: Date: 7 Jun 91 16:27:40 GMT Sender: news@crdgw1.crd.ge.com Reply-To: barnett@crdgw1.ge.com Organization: GE Corp. R & D, Schenectady, NY Lines: 102 Here is my first perl script. Be gentle - gang. You can use this to execute "df -t 4.2" on hundreds of machines. The script will spawn off several processes (default maximum: 6) and run an rsh command in each. When each one terminates, a new rsh is executed. So the rsh processes run in parallel. The name of the machine is printed at the beginning of each line. #!/usr/bin/perl # -*-Perl-*- # This is a mux version of rsh # it will do several rsh commands in parallel # just the thing for doing "df -t 4.2" on 400 machines # # Bruce Barnett # (Thanks to Randal for hard part) # # default number of processes is 6, # but is resettable using the -m argument # # usage: # muxrsh [-m max] [-v] [-l username] command [args ...] ) { chop $machine; $verbose && printf "forking for %s with %d alive\n", $machine, &alive; &wait() if &alive > $maxpids; $pid = fork; die "fork: $!" unless defined $pid; if ( $pid) { # parent $pids{$pid} = $machine; } else { $results=`rsh -n $remoteuser $machine @ARGV 2>&1 `; # this makes sure "machine: " is before each line returned. @mresults = (split(/\n/, $results)); for $mresult (@mresults) { print "$machine: $mresult\n"; } exit 0; } } &wait() while &alive > 0; } #-------------- # main routine # $|++; # flush buffers on a line-by-line basis $verbose = 0; # assume verbose is off $maxpids = 6; # default maximum number of processes #$remoteuser="-l staff"; # default remote user $remoteuser=""; %pids = (); # zero out the pids &getswitches(); # get any switches/changes to defaults @ARGV || die "Must specify a command to be executed!\n"; $verbose && $remoteuser && print "remote user is $remoteuser\n"; $verbose && print "Max number of processes is $maxpids\n"; &doit(); # tuit 0; -- Bruce G. Barnett barnett@crdgw1.ge.com uunet!crdgw1!barnett