diff options
author | ivan <ivan> | 2004-02-13 04:11:27 +0000 |
---|---|---|
committer | ivan <ivan> | 2004-02-13 04:11:27 +0000 |
commit | a58b44c62073cc5d18789b694b9785333ea4b4f1 (patch) | |
tree | 4a55ba7b2f5c3908eecd15678ff9562dbe677228 | |
parent | 344a40460ac84896c05879ec388de63f1c51a08b (diff) |
0.08Net_SSH_0_08
-rw-r--r-- | Changes | 3 | ||||
-rw-r--r-- | SSH.pm | 37 |
2 files changed, 32 insertions, 8 deletions
@@ -1,5 +1,8 @@ Revision history for Perl extension Net::SSH. +0.08 Thu Feb 12 16:44:36 2004 + - replace ssh_cmd blocking read of STDOUT then STDERR with IO::Select + 0.07 Mon Aug 26 02:53:26 2002 - turn on -T to quiet pseudo-terminal warnings on OpenSSH v2 or later @@ -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 |