$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
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);
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;
=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 ) {
sub sshopen2 {
my($host, $reader, $writer, @command) = @_;
+ &_check_ssh_version unless defined $equalspace;
open2($reader, $writer, $ssh, '-o', 'BatchMode'.$equalspace.'yes', $host, @command);
}
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);
}
$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