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