Last active
September 26, 2016 02:42
-
-
Save eqhmcow/954c9749a8050e9f43eb9c5c5f2f351e to your computer and use it in GitHub Desktop.
quick and dirty ssh then sudo then run commands in a loop w/ Net::CLI::Interact and Parallel::ForkManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Net::CLI::Interact; | |
use Parallel::ForkManager; | |
# servers | |
my @doit = qw( server1 server2 server3 ); | |
my $cmd = "@ARGV"; | |
# fix up cmd; newlines -> semicolons | |
$cmd =~ s/\s*;\s*\n/ ; /g; | |
$cmd =~ s/\n/ ; /g; | |
$cmd =~ s/^\s*;\s*//; | |
my $pm = new Parallel::ForkManager(256); | |
warn "running $cmd against @doit servers"; | |
warn "is that ok"; | |
$_ = readline STDIN; | |
die unless m/^y/; | |
foreach my $server (@doit) { | |
my $pid = $pm->start() and next; | |
#my $b = "[$$] [$server]"; | |
my $b = "[$server]"; | |
print "$b ssh to $server\n"; | |
my $s = Net::CLI::Interact->new( | |
{ | |
transport => 'SSH', | |
personality => 'bash', | |
connect_options => { host => $server }, | |
log_config => { | |
dispatchers => ['screen'], | |
screen => | |
{ class => 'Log::Dispatch::Screen', min_level => 'debug', }, | |
}, | |
timeout => 300, | |
} | |
); | |
# see previous rev for alternate sudo approach | |
$s->macro('begin_privileged'); | |
$s->set_prompt('privileged'); | |
$s->cmd('passwd here'); | |
my $output = $s->cmd($cmd); | |
$output =~ s/^/$b /g; | |
$output =~ s/\n/\n$b /g; | |
print "$output\n"; | |
$pm->finish(); | |
} | |
$pm->wait_all_children(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this name.