| 1 | package POE::Component::Generic::Net::SSH2; | ||
| 2 | # $Id: SSH2.pm 762 2011-05-18 19:34:32Z fil $ | ||
| 3 | |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use POE::Component::Generic; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use POE::Component::Generic::Child; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | use Carp qw(carp croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use vars qw( @ISA $TIMEOUT $VERSION ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | use # hide from CPANTS | ||
| 13 | 0 | ▶ | Net::SSH2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | $VERSION = '0.1400'; | ||
| 16 | @ISA = qw( POE::Component::Generic ); | ||
| 17 | $TIMEOUT = 100; | ||
| 18 | |||
| 19 | require Net::SSH2; | ||
| 20 | if( $Net::SSH2::VERSION < 0.18 ) { | ||
| 21 | croak __PACKAGE__, " requires version Net::SSH2 0.18 or better"; | ||
| 22 | } | ||
| 23 | |||
| 24 | ########################################################## | ||
| 25 | # | ||
| 26 | # Create the worker object | ||
| 27 | # This is called from PoCo::Generic->spawn() | ||
| 28 | # We set-up everything for the package, so the user doesn't have to. | ||
| 29 | sub new | ||
| 30 | { | ||
| 31 | my( $package, %params ) = @_; | ||
| 32 | |||
| 33 | $params{package} ||= 'Net::SSH2'; | ||
| 34 | $params{factories} ||= [ qw( channel real_exec ) ]; | ||
| 35 | $params{packages} ||= {}; | ||
| 36 | $params{packages}{'Net::SSH2::Channel'}{ postbacks } | ||
| 37 | ||= [ qw(handler_stderr handler_stdout handler_closed) ]; | ||
| 38 | $params{postbacks} ||= { disconnect=>0, real_exec=>[1..4] }; | ||
| 39 | $params{alias} ||= 'net-ssh'; | ||
| 40 | $params{child_package} ||= join '::', __PACKAGE__, 'Child'; | ||
| 41 | |||
| 42 | $TIMEOUT = delete $params{timeout} if $params{timeout}; | ||
| 43 | |||
| 44 | $package->SUPER::new( %params ); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | ########################################################## | ||
| 49 | # This method converts the SomethingEvent hash into a linear | ||
| 50 | # argument list, so that the user doesn't have to. | ||
| 51 | sub exec | ||
| 52 | { | ||
| 53 | my( $self, $data, $command, %events ) = @_; | ||
| 54 | |||
| 55 | my @args = ( $data, $command ); | ||
| 56 | foreach my $ev ( qw(Stdout Stderr Closed Error) ) { | ||
| 57 | push @args, $self->__postback_argument( $ev, \%events ); | ||
| 58 | } | ||
| 59 | |||
| 60 | $self->real_exec( @args ); | ||
| 61 | } | ||
| 62 | |||
| 63 | ##################################################################### | ||
| 64 | # Child process worker | ||
| 65 | package POE::Component::Generic::Net::SSH2::Child; | ||
| 66 | use strict; | ||
| 67 | |||
| 68 | use IO::Poll qw(POLLRDNORM POLLIN); | ||
| 69 | use Scalar::Util qw( reftype blessed ); | ||
| 70 | use vars qw( @ISA ); | ||
| 71 | |||
| 72 | sub DEBUG () { 0 } | ||
| 73 | |||
| 74 | @ISA = qw( POE::Component::Generic::Child ); | ||
| 75 | |||
| 76 | |||
| 77 | sub new | ||
| 78 | { | ||
| 79 | my $package = shift; | ||
| 80 | my $self = $package->SUPER::new( @_ ); | ||
| 81 | |||
| 82 | $self->{timeout} ||= $POE::Component::Generic::Net::SSH2::TIMEOUT; | ||
| 83 | |||
| 84 | $Net::SSH2::CHILD = $self; | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | ####################################### | ||
| 89 | # Poll stdin and the SSH connection | ||
| 90 | sub get_requests | ||
| 91 | { | ||
| 92 | my( $self ) = @_; | ||
| 93 | |||
| 94 | unless( $self->{ssh_channels} ) { # no registered SSH channels | ||
| 95 | return $self->SUPER::get_requests(); | ||
| 96 | } | ||
| 97 | DEBUG and warn "we have ssh_channel handlers"; | ||
| 98 | |||
| 99 | my $rv; | ||
| 100 | while( 1 ) { | ||
| 101 | $self->poll_ssh; # let SSH channels have a chance | ||
| 102 | |||
| 103 | $rv = $self->poll_stdin; | ||
| 104 | return $rv if defined $rv; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | ####################################### | ||
| 109 | sub poll_ssh | ||
| 110 | { | ||
| 111 | my( $self ) = @_; | ||
| 112 | |||
| 113 | my @poll; | ||
| 114 | while( my( $name, $hdef ) = each %{ $self->{ssh_channels} || {} } ) { | ||
| 115 | push @poll, { handle=>$hdef->{channel}, | ||
| 116 | events=>[ grep { $hdef->{$_} } qw( in ext ) ] | ||
| 117 | }; | ||
| 118 | } | ||
| 119 | return unless @poll; | ||
| 120 | DEBUG and warn "ssh_poll"; | ||
| 121 | # Blocking has to be off for polling | ||
| 122 | $self->{obj}->blocking( 0 ); | ||
| 123 | $self->{obj}->poll( $self->{timeout}, [ @poll ] ); | ||
| 124 | |||
| 125 | DEBUG and warn "ssh_poll returned ", 0+@poll, " events"; | ||
| 126 | |||
| 127 | my( $n, $buf ); | ||
| 128 | foreach my $poll ( @poll ) { | ||
| 129 | my $hdef = $self->{ssh_channels}{ $poll->{handle} }; | ||
| 130 | foreach my $ev ( qw( in ext ) ) { | ||
| 131 | next unless $poll->{revents}{$ev}; | ||
| 132 | |||
| 133 | if( $n = $hdef->{channel}->read( $buf, 1024, $ev eq 'ext' ) ) { | ||
| 134 | DEBUG and warn "$ev: $n bytes"; | ||
| 135 | $hdef->{$ev}->( $buf, $n ); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | if( $hdef->{channel}->eof ) { | ||
| 139 | DEBUG and warn "EOF"; | ||
| 140 | $hdef->{channel_closed}->(); | ||
| 141 | delete $self->{ssh_channels}{ $poll->{handle} }; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | ####################################### | ||
| 148 | sub __handler | ||
| 149 | { | ||
| 150 | my( $self, $channel, $event, $coderef ) = @_; | ||
| 151 | |||
| 152 | $self->{ssh_channels} ||= {}; | ||
| 153 | |||
| 154 | if( $coderef ) { | ||
| 155 | die "Must be a coderef" unless 'CODE' eq reftype $coderef; | ||
| 156 | $self->{ssh_channels}{$channel} ||= { channel=> $channel }; | ||
| 157 | $self->{ssh_channels}{$channel}{$event} = $coderef; | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | delete $self->{ssh_channels}{$channel}{$event}; | ||
| 161 | delete $self->{ssh_channels}{$channel} | ||
| 162 | if 1 == keys %{ $self->{ssh_channels}{$channel} }; | ||
| 163 | delete $self->{ssh_channels} if 0 == keys %{ $self->{ssh_channels} }; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | ####################################### | ||
| 168 | sub __remove_channel | ||
| 169 | { | ||
| 170 | my( $self, $channel ) = @_; | ||
| 171 | |||
| 172 | return unless $self->{ssh_channels}; | ||
| 173 | |||
| 174 | delete $self->{ssh_channels}{$channel}; | ||
| 175 | delete $self->{ssh_channels} if 0 == keys %{ $self->{ssh_channels} }; | ||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | ####################################### | ||
| 181 | # Poll STDIN for a request | ||
| 182 | # Return | ||
| 183 | # undef() when STDIN closed (parent shutdown) | ||
| 184 | # 0 nothing to do | ||
| 185 | # [ requests...] what to do | ||
| 186 | sub poll_stdin | ||
| 187 | { | ||
| 188 | my( $self ) = @_; | ||
| 189 | |||
| 190 | DEBUG and warn "poll_stdin ($self->{timeout})"; | ||
| 191 | my $poll = IO::Poll->new(); | ||
| 192 | |||
| 193 | $poll->mask( *STDIN, POLLIN ); | ||
| 194 | $poll->poll( $self->{timeout}/1000 ); | ||
| 195 | my $raw; | ||
| 196 | my $ev = $poll->events( *STDIN ); | ||
| 197 | DEBUG and warn "ev=$ev"; | ||
| 198 | return unless $ev; | ||
| 199 | |||
| 200 | # This should be encapsulated in Generic::Child somehow. | ||
| 201 | return 0 unless sysread ( STDIN, $raw, $self->{size} ); | ||
| 202 | return $self->{filter}->get([$raw]); | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | ##################################################################### | ||
| 211 | # Extend Net::SSH2 | ||
| 212 | # Net::SSH2 doesn't play nicely with subclassing, so we just throw | ||
| 213 | # Things into their namespace. | ||
| 214 | package # On 2 lines so the PAUSE indexer doesn't gripe | ||
| 215 | Net::SSH2; | ||
| 216 | |||
| 217 | use strict; | ||
| 218 | use vars qw( $CHILD ); | ||
| 219 | sub DEBUG () { 0 } | ||
| 220 | |||
| 221 | *true_channel = \&channel; | ||
| 222 | |||
| 223 | ################################# | ||
| 224 | { | ||
| 225 | no strict 'subs'; | ||
| 226 | *channel = sub { | ||
| 227 | my( $self, @args ) = @_; | ||
| 228 | # Blocking has to be on when setting up a channel | ||
| 229 | # And for ->exec and ->cmd, it seems, but ... | ||
| 230 | $self->blocking( 1 ); | ||
| 231 | my $obj = true_channel( $self, @args ); | ||
| 232 | warn "Can't create channel ".$self->error unless $obj; | ||
| 233 | return $obj; | ||
| 234 | }; | ||
| 235 | } | ||
| 236 | |||
| 237 | ################################# | ||
| 238 | # This method does the heavy-lifting for ->exec | ||
| 239 | sub real_exec | ||
| 240 | { | ||
| 241 | my( $self, $command, $on_stdout, $on_stderr, | ||
| 242 | $on_closed, $on_error ) = @_; | ||
| 243 | |||
| 244 | my $channel = $self->channel; | ||
| 245 | unless( $channel ) { | ||
| 246 | $on_error->( $self->error ) if $on_error; | ||
| 247 | return | ||
| 248 | } | ||
| 249 | DEBUG and warn "Created channel"; | ||
| 250 | |||
| 251 | $CHILD->__handler( $channel, 'in', $on_stdout ) if $on_stdout; | ||
| 252 | $CHILD->__handler( $channel, 'ext', $on_stderr ) if $on_stderr; | ||
| 253 | $CHILD->__handler( $channel, 'channel_closed', $on_closed ) if $on_closed; | ||
| 254 | # $CHILD->__handler( $channel, 'channel_error', $on_error ) if $on_error; | ||
| 255 | |||
| 256 | DEBUG and | ||
| 257 | warn "Exec $command"; | ||
| 258 | my $ok = $channel->exec( $command ); | ||
| 259 | |||
| 260 | if( $ok ) { | ||
| 261 | return $channel; | ||
| 262 | } | ||
| 263 | |||
| 264 | $CHILD->__remove_channel( $channel ); | ||
| 265 | warn "Couldn't exec $command"; | ||
| 266 | $on_error->( $self->error ) if $on_error; | ||
| 267 | return; | ||
| 268 | } | ||
| 269 | |||
| 270 | |||
| 271 | sub cmd | ||
| 272 | { | ||
| 273 | my( $self, $command, $input ) = @_; | ||
| 274 | |||
| 275 | my $channel = $self->channel; | ||
| 276 | unless( $channel ) { | ||
| 277 | return ( '', "Unable to create channel: ".$self->error ); | ||
| 278 | } | ||
| 279 | |||
| 280 | return $channel->cmd( $command, $input ); | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | ##################################################################### | ||
| 285 | # Extend Net::SSH2::Channel | ||
| 286 | # Net::SSH2 doesn't play nicely with subclassing, so we just throw | ||
| 287 | # things into their namespace. | ||
| 288 | package # on 2 lines so PAUSE indexer doesn't gripe | ||
| 289 | Net::SSH2::Channel; | ||
| 290 | |||
| 291 | use strict; | ||
| 292 | sub DEBUG () { 0 } | ||
| 293 | |||
| 294 | sub handler_stdout | ||
| 295 | { | ||
| 296 | my( $self, $coderef ) = @_; | ||
| 297 | |||
| 298 | DEBUG and warn "handler_stdout"; | ||
| 299 | $Net::SSH2::CHILD->__handler( $self, 'in', $coderef ); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub handler_stderr | ||
| 303 | { | ||
| 304 | my( $self, $coderef ) = @_; | ||
| 305 | |||
| 306 | DEBUG and warn "handler_stderr"; | ||
| 307 | $Net::SSH2::CHILD->__handler( $self, 'ext', $coderef ); | ||
| 308 | } | ||
| 309 | |||
| 310 | sub handler_closed | ||
| 311 | { | ||
| 312 | my( $self, $coderef ) = @_; | ||
| 313 | |||
| 314 | DEBUG and warn "handler_closed"; | ||
| 315 | $Net::SSH2::CHILD->__handler( $self, 'channel_closed', $coderef ); | ||
| 316 | } | ||
| 317 | |||
| 318 | sub cmd | ||
| 319 | { | ||
| 320 | my( $self, $command ) = @_; | ||
| 321 | |||
| 322 | DEBUG and warn "cmd: $command"; | ||
| 323 | |||
| 324 | return unless $self->exec( $command ); | ||
| 325 | |||
| 326 | my @ret = ( '', '' ); | ||
| 327 | |||
| 328 | $self->blocking( 0 ); | ||
| 329 | my @poll = ({ handle => $self, events => [ qw( in ext ) ] }); | ||
| 330 | do { | ||
| 331 | my( $buf, $n ); | ||
| 332 | $self->session->poll(100, \@poll); | ||
| 333 | if( $self->eof ) { | ||
| 334 | DEBUG and | ||
| 335 | warn "EOF"; | ||
| 336 | return @ret if wantarray; | ||
| 337 | return $ret[0]; | ||
| 338 | } | ||
| 339 | foreach my $ev ( qw( in ext ) ) { | ||
| 340 | next unless $poll[0]->{revents}->{$ev}; | ||
| 341 | |||
| 342 | DEBUG and warn "cmd: read $ev"; | ||
| 343 | if( $n = $self->read($buf, 1024, $ev eq 'ext') ) { | ||
| 344 | DEBUG and warn "$ev: $n bytes"; | ||
| 345 | $ret[ $ev eq 'in' ? 0 : 1 ] .= $buf; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | } while( 1 ); | ||
| 349 | # we'd never get here | ||
| 350 | } | ||
| 351 | |||
| 352 | no strict 'subs'; | ||
| 353 | *DESTROY = sub { | ||
| 354 | my( $self ) = @_; | ||
| 355 | $Net::SSH2::CHILD->__remove_channel( $self ) if $Net::SSH2::CHILD; | ||
| 356 | }; | ||
| 357 | |||
| 358 | 1; | ||
| 359 | |||
| 360 | __END__ | ||