Useful Script

Here is a script that one of my co-workers wrote.  It goes and does something remotely to a set of servers very quick like using children.  This script has inspired me to build children into any script where I am going to do things to a LOT of servers because it will go much crazy fast. Yes, I’m so excited about this script, that it makes me lose my grammar. 😉


use Parallel::ForkManager;
$start = time();
use Getopt::Long;
$delimiter=' ';
$result=GetOptions ('command=s' => \$command,"time" => \$time,"delimiter=s" => \$delimiter,"child=s" => \$child,"multiline" => \$multiline);

while (defined($line = )) {
push @all_data,$line

$pm = new Parallel::ForkManager($child);
for $data (@all_data) {
# Forks and returns the pid for the child:
my $pid = $pm->start and next;
$test=`ssh -q -o ConnectTimeout=5 $data '$command'`;
#$test =~ s/\n/ /g;

if ($multiline ne '0')
my @lines = split /\n/, $test;
foreach my $line (@lines) {
print "$data$delimiter$line\n";
print "$data$delimiter$test\n";

$pm->finish; # Terminates the child process
$end = time();
if ($time eq '1')
print "\nTime taken was ", ($end - $start), " seconds\n\n" ;
#print "\n\n\n\ndone\n\n\n\n";

Leave a Reply

Your email address will not be published. Required fields are marked *