| 1 | package XAS::Lib::SSH::Client::Exec; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.01'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Params::Validate qw(SCALAR CODEREF); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | use XAS::Class | ||||||||||
| 7 | 1 | debug => 0, | |||||||||
| 8 | version => $VERSION, | ||||||||||
| 9 | base => 'XAS::Lib::SSH::Client', | ||||||||||
| 10 | accessors => 'exit_code exit_signal', | ||||||||||
| 11 | utils => 'trim', | ||||||||||
| 12 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | #use Data::Hexdumper; | ||||||||||
| 15 | #use Data::Dumper; | ||||||||||
| 16 | |||||||||||
| 17 | # ---------------------------------------------------------------------- | ||||||||||
| 18 | # Public Methods | ||||||||||
| 19 | # ---------------------------------------------------------------------- | ||||||||||
| 20 | |||||||||||
| 21 | sub setup { | ||||||||||
| 22 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
setup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 23 | |||||||||||
| 24 | 0 | my $output; | |||||||||
| 25 | |||||||||||
| 26 | # Merge stderr and stdout. | ||||||||||
| 27 | |||||||||||
| 28 | 0 | $self->chan->ext_data('merge'); | |||||||||
| 29 | |||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | sub run { | ||||||||||
| 33 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 34 | 0 | my ($command) = $self->validate_params(\@_, [1] ); | |||||||||
| 35 | |||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | sub call { | ||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
call: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 40 | 0 | my ($command, $parser) = $self->validate_params(\@_, [ | |||||||||
| 41 | { type => SCALAR }, | ||||||||||
| 42 | { type => CODEREF }, | ||||||||||
| 43 | ]); | ||||||||||
| 44 | |||||||||||
| 45 | 0 | my @output; | |||||||||
| 46 | |||||||||||
| 47 | # execute a command, retrieve the output and dispatch to a parser. | ||||||||||
| 48 | |||||||||||
| 49 | 0 | $self->chan->pty('vt100'); | |||||||||
| 50 | 0 | $self->chan->exec($command); | |||||||||
| 51 | |||||||||||
| 52 | 0 | $self->{exit_code} = $self->chan->exit_status(); | |||||||||
| 53 | 0 | $self->{exit_signal} = $self->chan->exit_signal(); | |||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | while (my $line = $self->gets()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 0 | push(@output, trim($line)); | |||||||||
| 60 | |||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | } while ($self->pending()); | ||||||||||
| 64 | |||||||||||
| 65 | 0 | return $parser->(\@output); | |||||||||
| 66 | |||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # ---------------------------------------------------------------------- | ||||||||||
| 70 | # Private Methods | ||||||||||
| 71 | # ---------------------------------------------------------------------- | ||||||||||
| 72 | |||||||||||
| 73 | 1 | 1; | |||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||
| 76 | |||||||||||
| 77 | =head1 NAME | ||||||||||
| 78 | |||||||||||
| 79 | XAS::Lib::SSH::Client::Exec - A class to interact with the SSH Exec facility | ||||||||||
| 80 | |||||||||||
| 81 | =head1 SYNOPSIS | ||||||||||
| 82 | |||||||||||
| 83 | use XAS::Lib::SSH::Client::Exec; | ||||||||||
| 84 | |||||||||||
| 85 | my $client = XAS::Lib::SSH::Client::Exec->new( | ||||||||||
| 86 | -host => 'test-xen-01', | ||||||||||
| 87 | -username => 'root', | ||||||||||
| 88 | -password => 'secret', | ||||||||||
| 89 | ); | ||||||||||
| 90 | |||||||||||
| 91 | $client->connect(); | ||||||||||
| 92 | |||||||||||
| 93 | my @vms = $client->call('xe vm-list params', sub { | ||||||||||
| 94 | my $output = shift; | ||||||||||
| 95 | ... | ||||||||||
| 96 | }); | ||||||||||
| 97 | |||||||||||
| 98 | $client->disconnect(); | ||||||||||
| 99 | |||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||
| 101 | |||||||||||
| 102 | The module uses the SSH Exec subsystem to execute commands. Which means it | ||||||||||
| 103 | executes a procedure on a remote host and parses the resulting output. This | ||||||||||
| 104 | module inherits from L<XAS::Lib::SSH::Client|XAS::Lib::SSH::Client>. | ||||||||||
| 105 | |||||||||||
| 106 | =head1 METHODS | ||||||||||
| 107 | |||||||||||
| 108 | =head2 setup | ||||||||||
| 109 | |||||||||||
| 110 | This method will set up the environment to execute commands using the exec | ||||||||||
| 111 | subsystem on a remote system. | ||||||||||
| 112 | |||||||||||
| 113 | =head2 run($command) | ||||||||||
| 114 | |||||||||||
| 115 | This method does nothing. | ||||||||||
| 116 | |||||||||||
| 117 | =head2 call($command, $parser) | ||||||||||
| 118 | |||||||||||
| 119 | This method executes the command on the remote host and parses the output. | ||||||||||
| 120 | |||||||||||
| 121 | =over 4 | ||||||||||
| 122 | |||||||||||
| 123 | =item B<$command> | ||||||||||
| 124 | |||||||||||
| 125 | The command string to be executed. | ||||||||||
| 126 | |||||||||||
| 127 | =item B<$parser> | ||||||||||
| 128 | |||||||||||
| 129 | A coderef to the parser that will parse the returned data. The parser | ||||||||||
| 130 | will accept one parameter which is a reference to that data. | ||||||||||
| 131 | |||||||||||
| 132 | =back | ||||||||||
| 133 | |||||||||||
| 134 | The assumption with this method is that the remote command will return some | ||||||||||
| 135 | sort of parsable data stream. After the data has been parsed the results is | ||||||||||
| 136 | returned to the caller. | ||||||||||
| 137 | |||||||||||
| 138 | =head2 exit_code | ||||||||||
| 139 | |||||||||||
| 140 | Returns the exit code from the remote process. | ||||||||||
| 141 | |||||||||||
| 142 | =head2 exit_signal | ||||||||||
| 143 | |||||||||||
| 144 | Returns the exit signal from the remote process. | ||||||||||
| 145 | |||||||||||
| 146 | =head1 SEE ALSO | ||||||||||
| 147 | |||||||||||
| 148 | =over 4 | ||||||||||
| 149 | |||||||||||
| 150 | =item L<XAS|XAS> | ||||||||||
| 151 | |||||||||||
| 152 | =back | ||||||||||
| 153 | |||||||||||
| 154 | =head1 AUTHOR | ||||||||||
| 155 | |||||||||||
| 156 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 157 | |||||||||||
| 158 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 159 | |||||||||||
| 160 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||
| 161 | |||||||||||
| 162 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 163 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 164 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 165 | |||||||||||
| 166 | =cut | ||||||||||