From a58b44c62073cc5d18789b694b9785333ea4b4f1 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 13 Feb 2004 04:11:27 +0000 Subject: [PATCH] 0.08 --- Changes | 3 +++ SSH.pm | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Changes b/Changes index 15bb061..f698c53 100644 --- a/Changes +++ b/Changes @@ -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 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 -- 2.11.0