Interact.pm

filter: Statementstmt 81.1% 43 / 53 Branchbran 50.0% 4 / 8 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 78.5% 11 / 14 Podpod 40.0% 2 / 5 totaltotal 66.6% 62 / 93 CC 9 SCAR 16.6
CC
9
Cov
71%
set_phrasebook
20.0
set_global_log_at
4.5
BUILD
2.0
CRAP 5.3
« Interact/Transport/Telnet.pm ↑ Summary Interact/Transport/Wrapper/Net_Telnet.pm »
1 package Net::CLI::Interact;
2 {
3 1 $Net::CLI::Interact::VERSION = '2.200007_001';
4 }
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MooX::Types::MooseLike::Base qw(InstanceOf Maybe Str HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Net::CLI::Interact::Role::Engine';
12  
13 1 has 'my_args' => (
14 is => 'rwp',
15 isa => HashRef,
16 );
17  
18 # stash all args in my_args
19 sub BUILDARGS {
20 2 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
21  
22 # accept single hash ref or naked hash
23 2 ▶ my $params = (ref {} eq ref $args[0] ? $args[0] : {@args});
Branch: 1 branch
BranchTrueFalseTotal
ref {} eq ref $args[0] ? : 1 1 2
24  
25 2 return { my_args => $params };
26 }
27  
28 sub default_log_categories {
29 0 ▶ return (qw/dialogue dump engine object phrasebook prompt transport/);
Subroutine default_log_categories: not called CC=1 SCAR=6.9
Pod undocumented
30 }
31  
32 1 has 'log_at' => (
33 is => 'rw',
34 isa => Maybe[Str],
35 default => quote_sub(q[ $ENV{'NCI_LOG_AT'} ]),
36 trigger => \&set_global_log_at,
37 );
38  
39 sub set_global_log_at {
40 2 ▶ my ($self, $level) = @_;
Subroutine set_global_log_at: called CC=3 SCAR=15.1
Pod documented
41 2 ▶ return unless defined $level and length $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and length $level 2 0 2
Condition33%1/3
defined $level and length $level
!ll&&!rl&&r
200
MC/DC0%0/2
defined $level and length $level
defined $level length $level
Truth table33%1/3
A and B
A= defined $levelB= length $level
ABresult
0X0
100
111
42 $self->logger->log_flags({
43 0 map {$_ => $level} default_log_categories()
44 });
45 }
46  
47 sub BUILD {
48 2 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=7.1
Pod undocumented
49 2 $self->set_global_log_at($self->log_at);
50 2 ▶ $self->logger->log('engine', 'notice',
Condition50%1/2
$Net::CLI::Interact::VERSION || 'devel'
l!l
20
MC/DC0%0/1
$Net::CLI::Interact::VERSION || 'devel'
$Net::CLI::Interact::VERSION
Truth table50%1/2
A
A= $Net::CLI::Interact::VERSION
Aresult
11
00
51 sprintf "NCI loaded, version %s", ($Net::CLI::Interact::VERSION || 'devel'));
52 }
53  
54 1 has 'logger' => (
55 is => 'lazy',
56 isa => InstanceOf['Net::CLI::Interact::Logger'],
57 predicate => 1,
58 clearer => 1,
59 );
60  
61 sub _build_logger {
62 2 ▶ my $self = shift;
Subroutine _build_logger: called CC=1 SCAR=0.0
63 1 ▶ use Net::CLI::Interact::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 return Net::CLI::Interact::Logger->new($self->my_args);
65 }
66  
67 1 has 'phrasebook' => (
68 is => 'lazy',
69 isa => InstanceOf['Net::CLI::Interact::Phrasebook'],
70 predicate => 1,
71 clearer => 1,
72 );
73  
74 sub _build_phrasebook {
75 0 ▶ my $self = shift;
Subroutine _build_phrasebook: not called CC=1 SCAR=1.2
76 1 ▶ use Net::CLI::Interact::Phrasebook;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 return Net::CLI::Interact::Phrasebook->new({
78 0 %{ $self->my_args },
79 logger => $self->logger,
80 });
81 }
82  
83 # does not really *change* the phrasebook, just reconfig and nuke
84 sub set_phrasebook {
85 0 ▶ my ($self, $args) = @_;
Subroutine set_phrasebook: not called CC=4 SCAR=30.0
Pod documented
86 0 ▶ return unless defined $args and ref {} eq ref $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args and ref {} eq ref $args 0 0 0
Condition0%0/3
defined $args and ref {} eq ref $args
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args and ref {} eq ref $args
defined $args ref {} eq ref $args
Truth table0%0/3
A and B
A= defined $argsB= ref {} eq ref $args
ABresult
0X0
100
111
87 0 $self->my_args->{$_} = $args->{$_} for keys %$args;
88 0 $self->clear_phrasebook;
89 }
90  
91 1 has 'transport' => (
92 is => 'lazy',
93 isa => quote_sub(q{ $_[0]->isa('Net::CLI::Interact::Transport') }),
94 predicate => 1,
95 clearer => 1,
96 );
97  
98 sub _build_transport {
99 2 ▶ my $self = shift;
Subroutine _build_transport: called CC=2 SCAR=7.0
100 2 ▶ die 'missing transport' unless exists $self->my_args->{transport};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->my_args->{"transport"} 0 2 2
101 2 my $tpt = 'Net::CLI::Interact::Transport::'. $self->my_args->{transport};
102 2 Class::Load::load_class($tpt);
103 return $tpt->new({
104 2 %{ $self->my_args },
105 logger => $self->logger,
106 });
107 }
108  
109 1 1;
110  
111 # ABSTRACT: Toolkit for CLI Automation
112  
113  
114 __END__
115 =pod
116  
117 =head1 NAME
118  
119 Net::CLI::Interact - Toolkit for CLI Automation
120  
121 =head1 PURPOSE
122  
123 This module exists to support developers of applications and libraries which
124 must interact with a command line interface.
125  
126 =head1 SYNOPSIS
127  
128 use Net::CLI::Interact;
129
130 my $s = Net::CLI::Interact->new({
131 personality => 'cisco',
132 transport => 'Telnet',
133 connect_options => { host => '192.0.2.1' },
134 });
135
136 # respond to a usename/password prompt
137 $s->macro('to_user_exec', {
138 params => ['my_username', 'my_password'],
139 });
140
141 my $interfaces = $s->cmd('show ip interfaces brief');
142
143 $s->macro('to_priv_exec', {
144 params => ['my_password'],
145 });
146 # matched prompt is updated automatically
147
148 # paged output is slurped into one response
149 $s->macro('show_run');
150 my $config = $s->last_response;
151  
152 =head1 DESCRIPTION
153  
154 Automating command line interface (CLI) interactions is not a new idea, but
155 can be tricky to implement. This module aims to provide a simple and
156 manageable interface to CLI interactions, supporting:
157  
158 =over 4
159  
160 =item *
161  
162 SSH, Telnet and Serial-Line connections
163  
164 =item *
165  
166 Unix and Windows support
167  
168 =item *
169  
170 Reuseable device command phrasebooks
171  
172 =back
173  
174 If you're a new user, please read the
175 L<Tutorial|Net::CLI::Interact::Manual::Tutorial>. There's also a
176 L<Cookbook|Net::CLI::Interact::Manual::Cookbook> and a L<Phrasebook
177 Listing|Net::CLI::Interact::Manual::Phrasebook>. For a more complete worked
178 example check out the L<Net::Appliance::Session> distribution, for which this
179 module was written.
180  
181 =head1 INTERFACE
182  
183 =head2 new( \%options )
184  
185 Prepares a new session for you, but will not connect to any device. On
186 Windows platforms, you B<must> download the C<plink.exe> program, and pass
187 its location to the C<app> parameter. Other options are:
188  
189 =over 4
190  
191 =item C<< personality => $name >> (required)
192  
193 The family of device command phrasebooks to load. There is a built-in library
194 within this module, or you can provide a search path to other libraries. See
195 L<Net::CLI::Interact::Manual::Phrasebook> for further details.
196  
197 =item C<< transport => $backend >> (required)
198  
199 The name of the transport backend used for the session, which may be one of
200 L<Telnet|Net::CLI::Interact::Transport::Telnet>,
201 L<SSH|Net::CLI::Interact::Transport::SSH>, or
202 L<Serial|Net::CLI::Interact::Transport::Serial>.
203  
204 =item C<< connect_options => \%options >>
205  
206 If the transport backend can take any options (for example the target
207 hostname), then pass those options in this value as a hash ref. See the
208 respective manual pages for each transport backend for further details.
209  
210 =item C<< log_at => $log_level >>
211  
212 To make using the C<logger> somewhat easier, you can pass this argument the
213 name of a log I<level> (such as C<debug>, C<info>, etc) and all logging in the
214 library will be enabled at that level. Use C<debug> to learn about how the
215 library is working internally. See L<Net::CLI::Interact::Logger> for a list of
216 the valid level names.
217  
218 =item C<< timeout => $seconds >>
219  
220 Configures a default timeout value, in seconds, for interaction with the
221 remote device. The default is 10 seconds. You can also set timeout on a
222 per-command or per-macro call (see below).
223  
224 Note that this does not (currently) apply to the initial connection.
225  
226 =back
227  
228 =head2 cmd( $command )
229  
230 Execute a single command statement on the connected device, and consume output
231 until there is a match with the current I<prompt>. The statement is executed
232 verbatim on the device, with a newline appended.
233  
234 In scalar context the C<last_response> is returned (see below). In list
235 context the gathered response is returned as a list of lines. In both cases
236 your local platform's newline character will end all lines.
237  
238 =head2 macro( $name, \%options? )
239  
240 Execute the commands contained within the named Macro, which must be loaded
241 from a Phrasebook. Options to control the output, including variables for
242 substitution into the Macro, are passed in the C<%options> hash reference.
243  
244 In scalar context the C<last_response> is returned (see below). In list
245 context the gathered response is returned as a list of lines. In both cases
246 your local platform's newline character will end all lines.
247  
248 =head2 last_response
249  
250 Returns the gathered output after the most recent C<cmd> or C<macro>. In
251 scalar context all data is returned. In list context the gathered response is
252 returned as a list of lines. In both cases your local platform's newline
253 character will end all lines.
254  
255 =head2 transport
256  
257 Returns the L<Transport|Net::CLI::Interact::Transport> backend which was
258 loaded based on the C<transport> option to C<new>. See the
259 L<Telnet|Net::CLI::Interact::Transport::Telnet>,
260 L<SSH|Net::CLI::Interact::Transport::SSH>, or
261 L<Serial|Net::CLI::Interact::Transport::Serial> documentation for further
262 details.
263  
264 =head2 phrasebook
265  
266 Returns the Phrasebook object which was loaded based on the C<personality>
267 option given to C<new>. See L<Net::CLI::Interact::Phrasebook> for further
268 details.
269  
270 =head2 set_phrasebook( \%options )
271  
272 Allows you to (re-)configure the loaded phrasebook, perhaps changing the
273 personality or library, or other properties. The C<%options> Hash ref should
274 be any parameters from the L<Phrasebook|Net::CLI::Interact::Phrasebook>
275 module, but at a minimum must include a C<personality>.
276  
277 =head2 set_default_contination( $macro_name )
278  
279 Briefly, a Continuation handles the slurping of paged output from commands.
280 See the L<Net::CLI::Interact::Phrasebook> documentation for further details.
281  
282 Pass in the name of a defined Contination (Macro) to enable paging handling as
283 a default for all sent commands. This is an alternative to describing the
284 Continuation format in each Macro.
285  
286 To unset the default Continuation, call the C<clear_default_continuation>
287 method.
288  
289 =head2 logger
290  
291 This is the application's L<Logger|Net::CLI::Interact::Logger> object. A
292 powerful logging subsystem is available to your application, built upon the
293 L<Log::Dispatch> distribution. You can enable logging of this module's
294 processes at various levels, or add your own logging statements.
295  
296 =head2 set_global_log_at( $level )
297  
298 To make using the C<logger> somewhat easier, you can pass this method the
299 name of a log I<level> (such as C<debug>, C<info>, etc) and all logging in the
300 library will be enabled at that level. Use C<debug> to learn about how the
301 library is working internally. See L<Net::CLI::Interact::Logger> for a list of
302 the valid level names.
303  
304 =head1 FUTHER READING
305  
306 =head2 Prompt Matching
307  
308 Whenever a command statement is issued, output is slurped until a matching
309 prompt is seen in that output. Control of the Prompts is shared between the
310 definitions in L<Net::CLI::Interact::Phrasebook> dictionaries, and methods of
311 the L<Net::CLI::Interact::Role::Prompt> core component. See that module's
312 documentation for further details.
313  
314 =head2 Actions and ActionSets
315  
316 All commands and macros are composed from their phrasebook definitions into
317 L<Actions|Net::CLI::Interact::Action> and
318 L<ActionSets|Net::CLI::Interact::ActionSet> (iterable sequences of Actions).
319 See those modules' documentation for further details, in case you wish to
320 introspect their structures.
321  
322 =head1 COMPOSITION
323  
324 See the following for further interface details:
325  
326 =over 4
327  
328 =item *
329  
330 L<Net::CLI::Interact::Role::Engine>
331  
332 =back
333  
334 =head1 AUTHOR
335  
336 Oliver Gorwits <oliver@cpan.org>
337  
338 =head1 COPYRIGHT AND LICENSE
339  
340 This software is copyright (c) 2014 by Oliver Gorwits.
341  
342 This is free software; you can redistribute it and/or modify it under
343 the same terms as the Perl 5 programming language system itself.
344  
345 =cut
346  
« Interact/Transport/Telnet.pm ↑ Summary Interact/Transport/Wrapper/Net_Telnet.pm »