1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
package Net::OpenSSH::Compat::Perl;
our $VERSION = '0.08';
use strict;
use warnings;
use Carp ();
use Net::OpenSSH;
use Net::OpenSSH::Constants qw(OSSH_MASTER_FAILED OSSH_SLAVE_CMD_FAILED);
require Exporter;
our @ISA = qw(Exporter);
our @CARP_NOT = qw(Net::OpenSSH);
my $supplant;
my $session_id = 1;
our %DEFAULTS = ( session => [protocol => 2,
strict_host_key_checking => 'no'],
connection => [] );
sub import {
my $class = shift;
if (!$supplant and
$class eq __PACKAGE__ and
grep($_ eq ':supplant', @_)) {
$supplant = 1;
for my $end ('') { #, qw(Channel SFTP Dir File)) {
my $this = __PACKAGE__;
my $pkg = "Net::SSH::Perl";
my $file = "Net/SSH/Perl";
if ($end) {
$this .= "::$end";
$pkg .= "::$end";
$file .= "/$end";
}
$INC{$file . '.pm'} = __FILE__;
no strict 'refs';
@{"${pkg}::ISA"} = ($this);
${"${pkg}::VERSION"} = __PACKAGE__->version;
}
}
__PACKAGE__->export_to_level(1, $class,
grep $_ ne ':supplant', @_);
}
sub version { "1.34 (".__PACKAGE__."-$VERSION)" }
sub new {
my $class = shift;
my $host = shift;
my $cfg = Net::OpenSSH::Compat::Perl::Config->new(@_);
my $cpt = { host => $host,
state => 'new',
cfg => $cfg,
session_id => $session_id++ };
bless $cpt, $class;
}
sub _entry_method {
my $n = 1;
my $last = 'unknown';
while (1) {
my $sub = (caller $n++)[3];
$sub =~ /^Net::OpenSSH::Compat::(?:\w+::)?(\w+)$/ or last;
$last = $1;
}
$last;
}
sub _check_state {
my ($cpt, $expected) = @_;
my $state = $cpt->{state};
return 1 if $expected eq $state;
my $method = $cpt->_entry_method;
my $class = ref $cpt;
Carp::croak qq($class object can't do "$method" on state $state);
return
}
sub _check_error {
my $cpt = shift;
my $ssh = $cpt->{ssh};
return if (!$ssh->error or $ssh->error == OSSH_SLAVE_CMD_FAILED);
my $method = $cpt->_entry_method;
$cpt->{state} = 'failed' if $ssh->error == OSSH_MASTER_FAILED;
Carp::croak "$method failed: " . $ssh->error;
}
sub login {
my ($cpt, $user, $password, $suppress_shell) = @_;
$cpt->_check_state('new');
$cpt->{user} = $user;
$cpt->{password} = '*****' if defined $password;
$cpt->{suppress_shell} = $suppress_shell;
my @args = (host => $cpt->{host}, @{$DEFAULTS{connection}});
push @args, user => $user if defined $user;
push @args, password => $password if defined $password;
my $cfg = $cpt->{cfg};
push @args, port => $cfg->{port} if defined $cfg->{port};
push @args, batch_mode => 1 unless $cfg->{interactive};
my @more;
push @more, 'UsePrivilegedPort=yes' if $cfg->{privileged};
push @more, "Ciphers=$cfg->{ciphers}" if defined $cfg->{ciphers};
push @more, "Compression=$cfg->{compression}" if defined $cfg->{compression};
push @more, "CompressionLevel=$cfg->{compression_level}" if defined $cfg->{compression_level};
push @more, "StrictHostKeyChecking=$cfg->{strict_host_key_checking}" if defined $cfg->{strict_host_key_checking};
if ($cfg->{identity_files}) {
push @more, "IdentityFile=$_" for @{$cfg->{identity_files}};
}
if ($cfg->{options}) {
push @more, @{$cfg->{options}};
}
push @args, master_opts => [map { -o => $_ } @more];
# warn "args: @args";
my $ssh = $cpt->{ssh} = Net::OpenSSH->new(@args);
if ($ssh->error) {
$ssh->{state} = 'failed';
$ssh->die_on_error;
}
$cpt->{state} = 'connected';
}
sub cmd {
my ($cpt, $cmd, $stdin) = @_;
$cpt->_check_state('connected');
my $ssh = $cpt->{ssh};
$stdin = '' unless defined $stdin;
local $?;
my ($out, $err) = $ssh->capture2({stdin_data => $stdin}, $cmd);
$cpt->_check_error;
return ($out, $err, ($? >> 8));
}
sub shell {
my $cpt = shift;
$cpt->_check_state('connected');
my $ssh = $cpt->{ssh};
my $tty = $cpt->{cfg}{use_pty};
$tty = 1 unless defined $tty;
$ssh->system({tty => $tty});
}
sub config { shift->{cfg} }
sub debug { Carp::carp("@_") if shift->{cfg}{debug} }
sub session_id { shift->{session_id} }
my $make_missing_methods = sub {
my $pkg = caller;
my $faked = $pkg;
$faked =~ s/^Net::OpenSSH::Compat::/Net::SSH::/;
for (@_) {
my $name = $_;
no strict 'refs';
*{$pkg.'::'.$name} = sub {
Carp::croak("method ${faked}::$name is not implemented by $pkg, report a bug if you want it supported!");
}
}
};
$make_missing_methods->(qw(register_handler
sock
incomming_data
packet_start));
package Net::OpenSSH::Compat::Perl::Config;
my %option_perl2openssh = qw(protocol proto);
sub new {
my $class = shift;
my %opts = (@{$DEFAULTS{session}}, @_);
my %cfg = map { my $v = delete $opts{$_};
my $name = $option_perl2openssh{$_} || $_;
defined $v ? ($name, $v) : () } qw(port protocol debug interactive
privileged identity_files cipher
ciphers compression
compression_level use_pty
options strict_host_key_checking);
%opts and Carp::croak "unsupported configuration option(s) given: ".join(", ", keys %opts);
$cfg{proto} =~ /\b2\b/ or Carp::croak "Unsupported protocol version requested $cfg{proto}";
bless \%cfg, $class;
}
sub get { $_[0]->{$_[1]} }
sub set {
my ($cfg, $k, $v) = @_;
$cfg->{$k} = $v if @_ == 3;
$cfg->{$k};
}
sub DESTROY {};
$make_missing_methods->(qw(read_config merge_directive AUTOLOAD));
1;
__END__
=head1 NAME
Net::OpenSSH::Compat::Perl - Net::OpenSSH adapter for Net::SSH::Perl API compatibility
=head1 SYNOPSIS
use Net::OpenSSH::Compat::Perl qw(:supplant);
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new('host');
$ssh->login($user, $passwd);
my ($out, $err, $rc) = $ssh->cmd($cmd);
=head1 DESCRIPTION
This module implements a subset of L<Net::SSH::Perl> API on top of
L<Net::OpenSSH>.
After the module is loaded as...
use Net::OpenSSH::Compat::Perl qw(:supplant);
... it supplants the Net::SSH::Perl module as if it were installed on
the machine using L<Net::OpenSSH> under the hood to handle SSH
operations.
=head2 Setting defaults
The hash C<%Net::OpenSSH::Compat::Perl::DEFAULTS> can be used to set
default values for L<Net::OpenSSH> and other modules called under the
hood and otherwise not accessible through the Net::SSH::Perl API.
The entries currently supported are:
=over
=item connection => [ %opts ]
Extra options passed to C<Net::OpenSSH::new> constructor.
Example:
$Net::OpenSSH::Compat::SSH::Perl::DEFAULTS{connection} =
[ ssh_path => "/opt/SSH/bin/ssh" ];
=back
=head1 BUGS AND SUPPORT
B<This is a work in progress.>
C<register_handler> method is not supported.
Net::SSH::Perl submodules (i.e. L<Net::SSH::Perl::Channel>) are not emulated.
Anyway, if your Net::SSH::Perl script fails, fill a bug report at the CPAN
RT bugtracker
(L<https://rt.cpan.org/Ticket/Create.html?Queue=Net-OpenSSH-Compat>)
or just send me an e-mail with the details.
Include at least:
=over 4
=item 1 - The full source of the script
=item 2 - A description of what happens in your machine
=item 3 - What you thing it should be happening
=item 4 - What happens when you use the real Net::SSH::Perl
=item 5 - The version and name of your operating system
=item 6 - The version of the OpenSSH ssh client installed on your machine (C<ssh -V>)
=item 7 - The Perl version (C<perl -V>)
=item 8 - The versions of the Perl packages Net::OpenSSH, IO::Pty and this Net::OpenSSH::Compat.
=back
=head2 Git repository
The source code repository is at
L<https://github.com/salva/p5-Net-OpenSSH-Compat>.
=head2 My wishlist
If you like this module and you're feeling generous, take a look at my
Amazon Wish List: L<http://amzn.com/w/1WU1P6IR5QZ42>
Also consider contributing to the OpenSSH project this module builds
upon: L<http://www.openssh.org/donations.html>.
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2011, 2014-2016 by Salvador FandiE<ntilde>o (sfandino@yahoo.com)
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.
=cut
|