| 1 | package Data::Collector::Engine::OpenSSH; | ||||||||||
| 2 | { | ||||||||||
| 3 | 3 | $Data::Collector::Engine::OpenSSH::VERSION = '0.15'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: An OpenSSH engine for Data::Collector utilizing Net::OpenSSH | ||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Net::OpenSSH; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | extends 'Data::Collector::Engine'; | |||||||||
| 12 | |||||||||||
| 13 | 3 | has '+name' => ( default => 'OpenSSH' ); | |||||||||
| 14 | |||||||||||
| 15 | 3 | has 'host' => ( | |||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'Str', | ||||||||||
| 18 | predicate => 'has_host', | ||||||||||
| 19 | required => 1, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 3 | has 'user' => ( is => 'rw', isa => 'Str', predicate => 'has_user' ); | |||||||||
| 23 | 3 | has 'passwd' => ( is => 'rw', isa => 'Str', predicate => 'has_passwd' ); | |||||||||
| 24 | 3 | has 'port' => ( is => 'rw', isa => 'Int', predicate => 'has_port' ); | |||||||||
| 25 | 3 | has 'ssh' => ( | |||||||||
| 26 | is => 'ro', | ||||||||||
| 27 | isa => 'Net::OpenSSH', | ||||||||||
| 28 | lazy_build => 1, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | sub _build_ssh { | ||||||||
|
Subroutine
_build_ssh: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | my $self = shift; | |||||||||
| 33 | |||||||||||
| 34 | 1 | return $self->connect(); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 3 | ▶ | sub connect { | ||||||||
|
Subroutine
connect: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 38 | 3 | my $self = shift; | |||||||||
| 39 | 3 | my %data = (); | |||||||||
| 40 | 3 | foreach my $attr ( qw/ user passwd port / ) { | |||||||||
| 41 | 9 | my $predicate = "has_$attr"; | |||||||||
| 42 | 9 | ▶ | $self->$predicate and $data{$attr} = $self->$attr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 3 | my $ssh = Net::OpenSSH->new( $self->host, %data ); | |||||||||
| 46 | |||||||||||
| 47 | 3 | ▶ | $ssh->error and die "OpenSSH Engine connect failed: " . $ssh->error; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 2 | return $ssh; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | sub run { | ||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 1 | my ( $self, $cmd ) = @_; | |||||||||
| 53 | |||||||||||
| 54 | 1 | return $self->ssh->capture($cmd); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub pipe { | ||||||||
|
Subroutine
pipe: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 58 | 0 | my ( $self, $cmd, $params ) = @_; | |||||||||
| 59 | 0 | my ( $in, $out, $pid ) = $self->ssh->open2($cmd); | |||||||||
| 60 | |||||||||||
| 61 | 0 | print {$in} $params; | |||||||||
| 62 | 0 | close $in; | |||||||||
| 63 | |||||||||||
| 64 | 0 | my $output; | |||||||||
| 65 | 0 | ▶ | while ( <$out> ) { $output .= $_; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | close $out; | |||||||||
| 67 | |||||||||||
| 68 | 0 | return $output; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 72 | 3 | 1; | |||||||||
| 73 | |||||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | =pod | ||||||||||
| 77 | |||||||||||
| 78 | =head1 NAME | ||||||||||
| 79 | |||||||||||
| 80 | Data::Collector::Engine::OpenSSH - An OpenSSH engine for Data::Collector utilizing Net::OpenSSH | ||||||||||
| 81 | |||||||||||
| 82 | =head1 VERSION | ||||||||||
| 83 | |||||||||||
| 84 | version 0.15 | ||||||||||
| 85 | |||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||
| 87 | |||||||||||
| 88 | use Data::Collector; | ||||||||||
| 89 | |||||||||||
| 90 | my $collector = Data::Collector->new( | ||||||||||
| 91 | engine => 'OpenSSH', # the default | ||||||||||
| 92 | engine_args => { | ||||||||||
| 93 | user => 'me', | ||||||||||
| 94 | host => 'soymilkyway', | ||||||||||
| 95 | passwd => 'crow@MIDn1ght', | ||||||||||
| 96 | }, | ||||||||||
| 97 | ); | ||||||||||
| 98 | |||||||||||
| 99 | =head1 ATTRIBUTES | ||||||||||
| 100 | |||||||||||
| 101 | =head2 host(Str) | ||||||||||
| 102 | |||||||||||
| 103 | Host to connect to. B<Required>. | ||||||||||
| 104 | |||||||||||
| 105 | =head2 user(Str) | ||||||||||
| 106 | |||||||||||
| 107 | Username to connect with. Defaults to session user. | ||||||||||
| 108 | |||||||||||
| 109 | =head2 passwd(Str) | ||||||||||
| 110 | |||||||||||
| 111 | Password to be used in connection. As with the OpenSSH C<ssh> program, if a | ||||||||||
| 112 | password is ot provided, it will go over other methods (such as keys), so this | ||||||||||
| 113 | is not required. | ||||||||||
| 114 | |||||||||||
| 115 | =head2 ssh(Object) | ||||||||||
| 116 | |||||||||||
| 117 | Contains the L<Net::OpenSSH> object that is used. | ||||||||||
| 118 | |||||||||||
| 119 | =head1 SUBROUTINES/METHODS | ||||||||||
| 120 | |||||||||||
| 121 | =head2 connect | ||||||||||
| 122 | |||||||||||
| 123 | This method creates the Net::OpenSSH object and connects to the host. | ||||||||||
| 124 | |||||||||||
| 125 | =head2 run | ||||||||||
| 126 | |||||||||||
| 127 | This functions runs the given command on the host using ssh and returns the | ||||||||||
| 128 | results. | ||||||||||
| 129 | |||||||||||
| 130 | =head2 pipe | ||||||||||
| 131 | |||||||||||
| 132 | Pipes your request to the command. Gets the command to run, returns the output | ||||||||||
| 133 | of that command. | ||||||||||
| 134 | |||||||||||
| 135 | =head1 AUTHOR | ||||||||||
| 136 | |||||||||||
| 137 | Sawyer X <xsawyerx@cpan.org> | ||||||||||
| 138 | |||||||||||
| 139 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 140 | |||||||||||
| 141 | This software is copyright (c) 2012 by Sawyer X. | ||||||||||
| 142 | |||||||||||
| 143 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 144 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 145 | |||||||||||
| 146 | =cut | ||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | __END__ | ||||||||||