-
Notifications
You must be signed in to change notification settings - Fork 1
/
spork
executable file
·125 lines (113 loc) · 3.11 KB
/
spork
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env perl
use threadeach;
use warnings;
use strict;
use Getopt::Long qw(:config bundling);
use Switch;
my $username=getlogin || getpwuid($<);
my $threaded=0;
my $filename="";
my $debug =0;
my $notint =0; #not interactive - shortcut for threading and running without needing interaction
GetOptions (
'username|u=s' => \$username,
'threaded|t:1' => \$threaded,
'filename|f=s' => \$filename,
'debug|d:1' => \$debug,
'notinteractive|n'=> \$notint,
'help|h' => \&usage,
);
my $command=shift || usage();
#check for passed values - which will be used as a list of machine names in lieu of the filename arg
my @lines=();
if(!-t STDIN){@lines=<>;}
if(!@lines)
{
open(F,$filename)
or die("Couldn't open file $filename for reading: $!\n");
@lines=<F>;
close(F)
or die("Couldn't close filehandle for $filename: $!\n");
}
if(!@lines){warn "No machine names found\n";usage();}
#get rid of newlines, empty lines, and whitespace in lines
chomp(@lines);
@lines=map{$a=$_;$a=~s/\s+//g;$a}@lines;
my @tmp=();foreach my $l(@lines){push(@tmp,$l) if $l;}
@lines=@tmp;
print "username=$username, threaded=$threaded, filename=$filename, command=$command\n" if $debug;
print "Host list:".join(",",@lines)."\n" if $debug;
#get the difference between the longest and shortest one (for pretty formatting)
my $longest=(sort(map {length($_)}@lines))[-1];
#decide how to run, threaded or not, and how many
switch($threaded)
{
case -1
{
threadall my $l(@lines){
runcmd_not_int($l);
}
}
case 1
{
threadeach my $l(@lines)
{
runcmd_not_int($l);
}
}
case {$threaded>0}
{
threadeach::threadsome(\&runcmd_not_int,$threaded,@lines);
}
else {run_looped()}
}
sub run_looped
{
my $iserr=shift ||0;
warn "Running in serial because of no threadeach library\n" if $iserr;
foreach my $line(@lines)
{
if($notint){&runcmd_not_int($line,$command);}
else{&runcmd_int($line,$command);}
}
}
sub runcmd_int
{
my $host=shift || return;
my $pad=" "x ($longest-length($host));
my $cmd="ssh $username\@$host \"$command\"";
print $cmd if $debug;
system($cmd);
}
#if it's not interactive, we can make the output nicer. :)
sub runcmd_not_int
{
my $host=shift || return;
open(CMD,"ssh $username\@$host \"$command\" |")
or die("Couldn't open command: $!");
my $pad=" "x ($longest-length($host));
while(<CMD>)
{
my $ret=$_;
chomp($ret);
print "$host:$pad$ret\n";
}
}
sub usage
{
print <<cEND;
Usage: $0 -f <filename> "command to run on remote host"
Options:
-u/--username username to run as
-t/--threaded Whether to run as threaded, optionally with the # of threads, or -1 to run all
-f/--filename filename to use as list of hosts
Example:
$0 -d -t 8 -u root -f myhostsfile "uptime"
will go to all the hosts listed in myhostsfile and run the "uptime" command with max 8 parallel threads
grep nameserver /etc/resolv.conf | awk '{print \$2}' | $0 'host google.com'
will go to all the nameservers in resolv.conf one at a time and run 'host google.com' on them
./$0 -f hostlist -t-1 -uroot uptime
will go to all machines in "hostlist" and run "uptime" with as many parallel threads as there are hosts
cEND
exit();
}