blib/lib/MYDan/Util/MIO/SSH.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 216 CC 21 SCAR 55.5
CC
21
Cov
0%
run
462.0
BEGIN
2.0
new
2.0
CRAP 256.2
1 package MYDan::Util::MIO::SSH;
2  
3 =head1 NAME
4  
5 MYDan::Util::MIO::SSH - Run multiple SSH commands in parallel.
6  
7 =head1 SYNOPSIS
8
9 use MYDan::Util::MIO::SSH;
10  
11 my @node = qw( host1 host2 ... );
12 my @cmd = qw( uptime );
13  
14 my $ssh = MYDan::Util::MIO::SSH->new( map { $_ => \@cmd } @node );
15 my $result = $ssh->run( max => 32, timeout => 300 );
16  
17 my $output = $result->{output};
18 my $error = $result->{error};
19  
20 =cut
21 use strict;
22 use warnings;
23  
24 use Carp;
25 use Expect;
26 use Tie::File;
27 use Fcntl qw( :flock );
28 use POSIX qw( :sys_wait_h );
29 use FindBin qw( $Script );
30 use Authen::OATH;
31 use Convert::Base32 qw( decode_base32 );
32  
33 use MYDan::Util::Alias;
34 use MYDan::Util::Hosts;
35 use MYDan::Util::Percent;
36 use MYDan::Util::Proxy;
37  
38 use base qw( MYDan::Util::MIO );
39  
40 our %RUN = %MYDan::Util::MIO::RUN;
41  
42 our $SSH;
43 BEGIN{
44 my $x = MYDan::Util::Alias->new()->alias( 'ssh' ) || 'ssh';
45 $SSH = $x . ' -o StrictHostKeyChecking=no -o NumberOfPasswordPrompts=1 -t';
46 };
47  
48 local $| = 1;
49  
50 sub new
51 {
52 my $self = shift;
53 $self->cmd( @_ );
54 }
55  
56 =head1 METHODS
57  
58 =head3 run( %param )
59  
60 Run ssh commands in parallel.
61 The following parameters may be defined in I<%param>:
62  
63 max : ( default 128 ) number of commands in parallel.
64 timeout : ( default 300 ) number of seconds allotted for each command.
65 sudo : ( default no sudo ) remote sudo user
66 user : ( default logname ) connect as user
67 pass : password
68  
69 =cut
70 sub run
71 {
72 local $/ = "\n";
73  
74 my $self = shift;
75 my @node = keys %$self;
76 my ( $run, %run, %result, %busy ) = ( 1, %RUN, @_ );
77 my ( $ext, $prompt ) = ( "$Script.$$", 'password:' );
78 my ( $max, $timeout, $user, $sudo, $pass, $input ) =
79 @run{ qw( max timeout user sudo pass input ) };
80  
81 my $percent = MYDan::Util::Percent->new( scalar @node, 'run ..' );
82  
83 $SIG{INT} = $SIG{TERM} = sub
84 {
85 print STDERR "killed\n";
86 $run = 0;
87 };
88  
89 my %hosts = MYDan::Util::Hosts->new()->match( @node );
90 my $p = MYDan::Util::Proxy->new( "$MYDan::PATH/etc/util/conf/proxy" );
91  
92 do
93 {
94 while ( @node && keys %busy < $max )
95 {
96 my $node = shift @node;
97 my $cmd = $self->{$node};
98 my $log = "/tmp/$node.$ext";
99  
100 my %x = $p->search( $hosts{$node} );
101  
102 my $ssh = $user ? "$SSH -l $user $hosts{$node} " : "$SSH $hosts{$node} ";
103 $ssh.= " -o ProxyCommand='nc -X 5 -x $x{$hosts{$node}} %h %p' " if $x{$hosts{$node}};
104  
105 if( @$cmd )
106 {
107 $ssh .= join ' ',
108 $sudo ? map { "sudo -p '$prompt' -u $sudo $_" } @$cmd : @$cmd;
109 }
110 else { $ssh .= " < $input"; }
111  
112 if ( $run{noop} ) { warn "$ssh\n"; next }
113 if ( my $pid = fork() ) { $busy{$pid} = [ $log, $node ]; next }
114
115 my ( $exp, %expect ) = Expect->new();
116  
117 if( $pass->{$node} && ref $pass->{$node} )
118 {
119 %expect = %{$pass->{$node}};
120 for( keys %expect )
121 {
122 next unless $expect{$_} =~ /googlecode\s*:\s*(\w+)/;
123 $expect{$_} = Authen::OATH->new->totp( decode_base32( $1 ));
124 }
125 }
126 elsif( $pass->{$node} )
127 {
128 $expect{assword} = $pass->{$node};
129 }
130  
131 $exp->log_stdout(0); #disable output to stdion (password: )
132 $exp->log_file( $log, 'w' );
133 warn "debug:$ssh\n" if $ENV{MYDan_DEBUG};
134 if ( $exp->spawn( $ssh ) )
135 {
136 $exp->expect( $timeout,
137 map{ my $v = $expect{$_};[ qr/$_/ => sub { $exp->send( "$v\n" ); exp_continue; } ] }keys %expect
138 );
139 }
140
141 exit 0;
142 }
143  
144 for ( keys %busy )
145 {
146 my $pid = waitpid( -1, WNOHANG );
147 next if $pid <= 0;
148  
149 my $stat = $? >> 8;
150  
151 next unless my $data = delete $busy{$pid};
152  
153 $percent->add()->print();
154  
155 my ( $log, $node ) = @$data;
156 tie my @log, 'Tie::File', $log;
157  
158 my @i = grep { $log[$_] =~ /$prompt/ } 0 .. $#log;
159 splice @log, 0, $i[-1] + 1 if @i;
160  
161 unless( $ENV{MYDan_DEBUG} )
162 {
163 @log = grep { $_ !~ m{Connection\ to.*?closed}xms } @log;
164 @log = grep { $_ !~ m{Warning: Permanently added .+ to the list of known hosts\.}m } @log;
165 @log = grep { $_ !~ m{Pseudo-terminal will not be allocated because stdin is not a terminal\.}m } @log;
166 pop @log if @log && $log[-1] =~ /^Last login: .*\d+:\d+:\d+/;
167 }
168  
169 my $end = $input ? '' : "--- $stat\n";
170 push @{ $result{output}{ join "\n", @log, $end } }, $node if @log;
171 unlink $log;
172 }
173 }
174 while $run && ( @node || %busy );
175  
176 kill 9, keys %busy;
177  
178 push @{ $result{output}{killed} }, map{ $busy{$_}[1]}keys %busy;
179 push @{ $result{output}{norun} }, @node;
180  
181 unlink glob "/tmp/*.$ext";
182 unlink $input if $input && -f $input;
183  
184 return wantarray ? %result : \%result;
185 }
186  
187 1;