Michael’s Interesting Stuff

Jump to content

Additional information

I'm listening to..

See more on Last.fm.

Tags

Archives


Useful Script

Mar 2012
23

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. 😉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/perl
 
use Parallel::ForkManager;
$start = time();
use Getopt::Long;
$time=0;
$delimiter=0;
$child=30;
$multiline=0;
$delimiter=' ';
$result=GetOptions ('command=s' => \$command,"time" => \$time,"delimiter=s" => \$delimiter,"child=s" => \$child,"multiline" => \$multiline);
 
 
while (defined($line = <STDIN>)) {
chomp($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;
chomp($data);
$test=`ssh -q -o ConnectTimeout=5 $data '$command'`;
chomp($test);
#$test =~ s/\n/ /g;
 
if ($multiline ne '0')
{
my @lines = split /\n/, $test;
foreach my $line (@lines) {
print "$data$delimiter$line\n";
}
}else
{
print "$data$delimiter$test\n";
}
 
 
 
 
$pm->finish; # Terminates the child process
}
 $pm->wait_all_children;
$end = time();
if ($time eq '1')
{
print "\nTime taken was ", ($end - $start), " seconds\n\n" ;
#print "\n\n\n\ndone\n\n\n\n";
}

Your Comment

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image

Credits

Template designed by praegnanz.de.