Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Multex - A Script For (Ex)ecuting The Same Command Across (Mult)iple Hosts

by bpoag (Monk)
on Mar 21, 2005 at 23:48 UTC ( [id://441344]=CUFP: print w/replies, xml ) Need Help??

#!/usr/bin/perl ## ## Multex v0.1 written 032105:1309 by Bowie J. Poag ## ## This runs a given command across several hosts, ## provided they have the main arbiter listed in ## their .rhosts file. :) ## ## Usage: multex <command> ## ## This script will only work if all the hosts you ## list in the @hosts array have an .rhosts file ## which allows connections from the system named ## in $arbiter. The command will always be invoked ## from the arbiter system, not necessarrily the ## system you're currently running it on---so make ## sure you have a copy of multex on the arbiter. :) ## $arbiter = "grover"; startupRoutine(); execRoutine(); spinDownRoutine(); sub startupRoutine() { $user=`whoami`; $whereAmI=`hostname`; chomp($user); chomp($whereAmI); @hosts=( "chapel.uconn.edu", "fcs93934.boulder.ibm.com", "stdclinic.skankytown.org", "grover", "cookiemonster.cisco1.cccp.gov", "budweiser", "icc.tcbc.eu", "nucleardefense.norad.disney.com", "thinktank.rand.com", "gilooly", "rampart", "skillz.tcbytreats.com", "radioaktivitat.de", "scrabble", ); print "Multex (on $whereAmI): Starting up..\n"; if ($#ARGV < 0 || $user ne "root") { die("\n Usage: $0 <command to distribute>\n\n Note: + You must be root to use this command. Example: $0 ls -l foo* \n\n +"); } while(++$argCounter!=$#ARGV+1) { $commandName=$commandName." ".$ARGV[$argCounter]; } $commandName=$ARGV[0].$commandName; } if($whereAmI eq $arbiter) { foreach $box (@hosts) { #print "Multex (on $whereAmI): Executing [$com +mandName] on [$box]..\n"; open (THISBOX,"rsh $box $commandName |"); @resultText=<THISBOX>; close (THISBOX); foreach $line (@resultText) { print "Multex: Output from $box: $line +"; } } } else { print "Multex (on $whereAmI): Distributing command..Pl +ease wait.\n"; open (ORIGINALBOX,"rsh $arbiter /usr/local/bin/multex +$commandName |"); @resultText=<ORIGINALBOX>; close(ORIGINALBOX); foreach $line (@resultText) { print "$line"; } } } sub spinDownRoutine() { print "Multex (on $whereAmI): Spinning down..\n"; exit; # bonk }

Replies are listed 'Best First'.
Re: Multex - A Script For (Ex)ecuting The Same Command Across (Mult)iple Hosts
by cazz (Pilgrim) on Mar 22, 2005 at 16:36 UTC
    Did you do a super search? This exact same problem has been discussed a few zillion times on PM.

    Though here are a few big issues I have with your implementation:
    1. you allow rsh as root?!? Are you insane?
    2. you are wasting quite a bit of memory by sucking the output of the command into resultText, since all you do is suck it up and spit it back out directly.
    3. you should use Parallel::ForkManager

      1) The script is in use among several machines that my co-worker and I administer on a private network, so, we're not too concerned about that angle. That being said, there's no reason why this script's use needs to be root-only. It's not like normal users cant rsh. You're free to edit to your hearts desire, of course.

      2) Pulling the output into a single array allows us a simple way to dump the contents into a separate report, which is what the foreach inside of execRoutine() was used for, originally...something I omitted from the code you're looking at.

      3) For our uses, we preferred the results to come back sequentially--The decision not to fork off the rsh calls was an intentional one. Besides, with the limited number of boxes we're adressing, we don't gain much in speed by forking anyway.

        Hi,

        Personally, I would avoid the entire rsh/rlogin/etc suite and use ssh (or similar). Other than that, I can see an issue with the script that don't appear to be an issue in your environment:

        • The script contacts servers A, B and C.
        • Server A completes the task, but server B doesn't respond... then Server C may not complete the task in the 'time window' if at all.
        • It would probably be better to spawn off the tasks to each individual server then display the output sequentially when the tasks are completed. That way the time is dramatically decreased.
        • This is assuming that B & C do not rely on the output of A.

        You deserve the ++ I gave ya though :)

        Jason L. Froebe

        Team Sybase member

        No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://441344]
Approved by thor
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-28 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found