-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdistributed-cmd.pl
More file actions
executable file
·63 lines (53 loc) · 1.63 KB
/
distributed-cmd.pl
File metadata and controls
executable file
·63 lines (53 loc) · 1.63 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use Net::SSH::Perl;
use Getopt::Long;
my $user;
my @hosts;
my $check;
my $command;
my $params={}; #Stores the parameters
GetOptions($params,
"hosts=s{1,}" =>\@hosts,
"check:s"=>\$check,
"user:s"=>\$user,
"command:s"=>\$command,
"help",
) or die &usage;
&usage unless not defined $params->{help} and defined $check and defined $command;
my $check_cmd = "$check > /dev/null 2>&1";
$user = "root" if not defined $user;
foreach(@hosts){
my $host=$_;
my $ssh = Net::SSH::Perl->new("$host");
eval{$ssh->login("$user");};
if (!$@){
print "####### ".$host." #######\n";
my ($stdout, $stderr, $exit) = $ssh->cmd("$check_cmd && (echo Running' $command' ; $command) && echo '$command run successfully'");
if(defined($stdout)){
chomp($stdout);
print ($stdout."\n");
} elsif(defined($stderr)){
print ("ERR:\n".$stderr."\n");
}
} else{
print "Failed to connect with server $host. Err: $@";
}
}
sub usage{
print shift."\n";
print <<EOF;
usage: distributed-cmd.pl [OPTIONS]
Runs a command by ssh on a list of servers given. Before run the command on the remote server a check command is executed, if returns 0 then the command will be run on the server.
Arguments:
--hosts : List of hosts to run the command
--check : Command line to decide if the server should or not run the command.
--command : Command line to run.
--user : user to connect with the servers. If not defined, by default is root.
--help : print this menu help
Example:
./distributed-cmd.pl --hosts srv1 srv2 --check "pidof apache2" --command "sudo /usr/sbin/service apache2 restart" --user root
EOF
exit 0;
}