From b44cc89d26bf6eb7374b8d4b3f5555bb7408f93d Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 20 Jun 2002 00:46:45 +0000 Subject: [PATCH] - fix version checking patch to detect at runtime, not compiletime - update ssh_cmd to accept named params & stdin_string --- Changes | 2 ++ SSH.pm | 68 +++++++++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/Changes b/Changes index fd7ed17..8d6dd25 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,8 @@ Revision history for Perl extension Net::SSH. 0.06 unreleased - patch from Anthony Awtrey to use s/=/ / in -o options again if OpenSSH v1 is detected + - fix patch to detect at runtime, not compiletime + - update ssh_cmd to accept named params & stdin_string 0.05 Fri Feb 15 15:46:00 2002 - brainfart mis-credit diff --git a/SSH.pm b/SSH.pm index b5391d5..0fe98cf 100644 --- a/SSH.pm +++ b/SSH.pm @@ -15,19 +15,6 @@ $DEBUG = 0; $ssh = "ssh"; -my $reader = IO::File->new(); -my $writer = IO::File->new(); -my $error = IO::File->new(); -open3($writer, $reader, $error, $ssh, '-V'); -my $ssh_version = <$error>; -chomp($ssh_version); -$ssh_version =~ s/.*OpenSSH[-|_](\w+?)\.\w+?\.\w+?.*/$1/g; -if ($ssh_version == 1) { - $equalspace = " "; -} else { - $equalspace = "="; -} - =head1 NAME Net::SSH - Perl extension for secure shell @@ -41,6 +28,13 @@ Net::SSH - Perl extension for secure shell issh('user@hostname', $command); ssh_cmd('user@hostname', $command); + ssh_cmd( { + user => 'user', + host => 'host.name', + command => 'command', + args => [ '-arg1', '-arg2' ], + stdin_string => "string\n", + } ); sshopen2('user@hostname', $reader, $writer, $command); @@ -65,6 +59,7 @@ Calls ssh in batch mode. sub ssh { my($host, @command) = @_; + &_check_ssh_version unless defined $equalspace; my @cmd = ($ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command); warn "[Net::SSH::ssh] executing ". join(' ', @cmd). "\n" if $DEBUG; @@ -89,22 +84,46 @@ sub issh { =item ssh_cmd [USER@]HOST, COMMAND [, ARGS ... ] +=item ssh_cmd OPTIONS_HASHREF + Calls ssh in batch mode. Throws a fatal error if data occurs on the command's STDERR. Returns any data from the command's STDOUT. +If using the hashref-style of passing arguments, possible keys are: + + user (optional) + host (requried) + command (required) + args (optional, arrayref) + stdin_string (optional) - written to the command's STDIN + =cut sub ssh_cmd { - my($host, @command) = @_; + my($host, $stdin_string, @command); + if ( ref($_[0]) ) { + my $opt = shift; + $host = $opt->{host}; + $host = $opt->{user}. '@'. $host if exists $opt->{user}; + @command = ( $opt->{command} ); + push @command, @{ $opt->{args} } if exists $opt->{args}; + my $stdin_string = $opt->{stdin_string}; + } else { + ($host, @command) = @_; + undef $stdin_string; + } my $reader = IO::File->new(); my $writer = IO::File->new(); my $error = IO::File->new(); - sshopen3( $host, $reader, $writer, $error, @command ) or die $!; + sshopen3( $host, $writer, $reader, $error, @command ) or die $!; + + print $writer $stdin_string if defined $stdin_string; + close $writer; local $/ = undef; - my $output_stream = <$writer>; + my $output_stream = <$reader>; my $error_stream = <$error>; if ( length $error_stream ) { @@ -123,6 +142,7 @@ Connects the supplied filehandles to the ssh process (in batch mode). sub sshopen2 { my($host, $reader, $writer, @command) = @_; + &_check_ssh_version unless defined $equalspace; open2($reader, $writer, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command); } @@ -134,6 +154,7 @@ Connects the supplied filehandles to the ssh process (in batch mode). sub sshopen3 { my($host, $writer, $reader, $error, @command) = @_; + &_check_ssh_version unless defined $equalspace; open3($writer, $reader, $error, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command); } @@ -143,6 +164,21 @@ sub _yesno { $x =~ /^y/i; } +sub _check_ssh_version { + my $reader = IO::File->new(); + my $writer = IO::File->new(); + my $error = IO::File->new(); + open3($writer, $reader, $error, $ssh, '-V'); + my $ssh_version = <$error>; + chomp($ssh_version); + $ssh_version =~ s/.*OpenSSH[-|_](\w+?)\.\w+?\.\w+?.*/$1/g; + if ($ssh_version == 1) { + $equalspace = " "; + } else { + $equalspace = "="; + } +} + =back =head1 EXAMPLE -- 2.11.0