X-Git-Url: http://git.freeside.biz/gitweb/?p=Net-SSH.git;a=blobdiff_plain;f=SSH.pm;h=7a421525beb86a87fe98e74eb35e162c8eccb742;hp=a47cc48811f2396384ab564eddba27313b6ba535;hb=a58b44c62073cc5d18789b694b9785333ea4b4f1;hpb=da4964ca55c1d21e4e557daa125b58c70e71c301 diff --git a/SSH.pm b/SSH.pm index a47cc48..7a42152 100644 --- a/SSH.pm +++ b/SSH.pm @@ -3,13 +3,15 @@ package Net::SSH; use strict; use vars qw($VERSION @ISA @EXPORT_OK $ssh $equalspace $DEBUG @ssh_options); use Exporter; +use POSIX ":sys_wait_h"; use IO::File; +use IO::Select; use IPC::Open2; use IPC::Open3; @ISA = qw(Exporter); @EXPORT_OK = qw( ssh issh ssh_cmd sshopen2 sshopen3 ); -$VERSION = '0.07'; +$VERSION = '0.08'; $DEBUG = 0; @@ -117,19 +119,38 @@ sub ssh_cmd { my $writer = IO::File->new(); my $error = IO::File->new(); - sshopen3( $host, $writer, $reader, $error, @command ) or die $!; + my $pid = sshopen3( $host, $writer, $reader, $error, @command ) or die $!; print $writer $stdin_string if defined $stdin_string; close $writer; - local $/ = undef; - my $output_stream = <$reader>; - my $error_stream = <$error>; + my $select = new IO::Select; + foreach ( $reader, $error ) { $select->add($_); } + + my($output_stream, $error_stream) = ('', ''); + while ( $select->count ) { + my @handles = $select->can_read; + foreach my $handle ( @handles ) { + my $buffer = ''; + my $bytes = sysread($handle, $buffer, 4096); + if ( !defined($bytes) ) { + waitpid($pid, WNOHANG); + die "[Net::SSH::ssh_cmd] $!" + }; + $select->remove($handle) if !$bytes; + if ( $handle eq $reader ) { + $output_stream .= $buffer; + } elsif ( $handle eq $error ) { + $error_stream .= $buffer; + } + } - if ( length $error_stream ) { - die "[Net:SSH::ssh_cmd] STDERR $error_stream"; } + waitpid($pid, WNOHANG); + + die "$error_stream" if length($error_stream); + return $output_stream; } @@ -230,7 +251,7 @@ OpenSSH v1. =head1 COPYRIGHT -Copyright (c) 2002 Ivan Kohler. +Copyright (c) 2004 Ivan Kohler. Copyright (c) 2002 Freeside Internet Services, LLC All rights reserved. This program is free software; you can redistribute it and/or modify it under