POE-Component-OpenSSH

 view release on metacpan or  search on metacpan

lib/POE/Component/OpenSSH.pm  view on Meta::CPAN

package POE::Component::OpenSSH;
# ABSTRACT: Non-blocking SSH Component for POE using Net::OpenSSH
$POE::Component::OpenSSH::VERSION = '0.11';
use strict;
use warnings;
use Carp 'croak';
use Net::OpenSSH;
use POE::Component::Generic;

sub _build_object {
    my ( $class, $opts ) = @_;

    return POE::Component::Generic->spawn(
        package        => 'Net::OpenSSH',
        object_options => $opts->{'args'},

        map +( $_ => $opts->{$_} ), qw<alias debug verbose error>,
    );
}

sub object   { shift->{'_object'} }
sub capture  { shift->{'_object'}->capture(@_)  }
sub capture2 { shift->{'_object'}->capture2(@_) }
sub system   { shift->{'_object'}->system(@_)   }
sub scp_get  { shift->{'_object'}->scp_get(@_)  }
sub scp_put  { shift->{'_object'}->scp_put(@_)  }
sub sftp     { shift->{'_object'}->sftp(@_)     }

sub new {
    my $class = shift;

    if ( @_ % 2 != 0 ) {
        croak 'Arguments must be in the form of key/value';
    }

    my %opts = (
        args    => [],
        options => {},
        error   => {},
        alias   => '',
        debug   => 0,
        verbose => 0,
        @_,
    );

    ref $opts{'args'}    eq 'ARRAY'
        or croak '"args" must be an arryref';

    ref $opts{'options'} eq 'HASH'
        or croak '"options" must be a hashref';

    ref $opts{'error'}   eq 'HASH'
        or croak '"error" must be a hashref';

    return bless { _object => $class->_build_object(\%opts) }, $class;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

POE::Component::OpenSSH - Non-blocking SSH Component for POE using Net::OpenSSH

=head1 VERSION

version 0.11

=head1 SYNOPSIS

Need non-blocking SSH? You like Net::OpenSSH? Try out this stuff right here.

    use POE::Component::OpenSSH;

    my $ssh = POE::Component::OpenSSH->new( args => [ $host, user => $user ] );
    $ssh->system( { event => 'read_system_output' }, 'w' );

Perhaps you want it with debugging and verbose of POE::Component::Generic

    my $ssh = POE::Component::OpenSSH->new(
        args    => [ 'root@host', passwd => $pass ],
        verbose => 1, # turns on POE::Component::Generic verbose
        debug   => 1, # turns on POE::Component::Generic debug
    );

What about setting timeout for Net::OpenSSH?

    my $ssh = POE::Component::OpenSSH->new(
        args => [ 'root@host', passwd => $pass, timeout => 10 ],
    );

=head1 DESCRIPTION

This module allows you to use SSH (via L<Net::OpenSSH>) in a non-blocking manner.

The only differences is that in the I<new()> method, you need to indicate
OpenSSH args in I<args>, and the first arg to a method should be a hashref that
includes an I<event> to reach with the result.

I kept having to write this small thing each time I needed non-blocking SSH in a
project. I got tired of it so I wrote this instead.

You might ask 'why put the args in an "args" attribute instead of straight away
attributes?' Because Net::OpenSSH has a lot of options and they may collide
with POE::Component::Generic's options and I don't feel like maintaining the
mess. It's on Github so you can patch it up if you want (I accept patches...
and foodstamps).

Here is a more elaborate example using L<MooseX::POE>:



( run in 2.012 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )