| 1 | package Net::CLI::Interact::Transport::Base; | ||||||||||
| 2 | { | ||||||||||
| 3 | 1 | $Net::CLI::Interact::Transport::Base::VERSION = '2.143070'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use MooX::Types::MooseLike::Base qw(InstanceOf); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | BEGIN { | ||||||||||
| 10 | 1 | ▶ | sub is_win32 { return ($^O eq 'MSWin32') } | ||||||||
|
Subroutine
is_win32: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | extends (is_win32() | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 13 | ? 'Net::CLI::Interact::Transport::Platform::Win32' | ||||||||||
| 14 | : 'Net::CLI::Interact::Transport::Platform::Unix'); | ||||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | { | ||||||||||
| 18 | 1 | package # hide from pause | |||||||||
| 19 | Net::CLI::Interact::Transport::Options; | ||||||||||
| 20 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | extends 'Net::CLI::Interact::Transport::Platform::Options'; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 1 | has 'logger' => ( | |||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | isa => InstanceOf['Net::CLI::Interact::Logger'], | ||||||||||
| 27 | required => 1, | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | 1 | 1; | |||||||||
| 31 | |||||||||||
| 32 | # ABSTRACT: Spawns an Interactive CLI Session | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | __END__ | ||||||||||
| 37 | =pod | ||||||||||
| 38 | |||||||||||
| 39 | =head1 NAME | ||||||||||
| 40 | |||||||||||
| 41 | Net::CLI::Interact::Transport::Base - Spawns an Interactive CLI Session | ||||||||||
| 42 | |||||||||||
| 43 | =head1 VERSION | ||||||||||
| 44 | |||||||||||
| 45 | version 2.143070 | ||||||||||
| 46 | |||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||
| 48 | |||||||||||
| 49 | This module provides a generic cross-platform API with the purpose of | ||||||||||
| 50 | interacting with a command line interface. | ||||||||||
| 51 | |||||||||||
| 52 | On Windows the L<IPC::Run> module is used and on Unix when L<IO::Pty> is | ||||||||||
| 53 | available (it requires a compiler) L<Net::Telnet>, else C<IPC::Run>. In all | ||||||||||
| 54 | cases, a program such as openssh is started and methods provided to send and | ||||||||||
| 55 | receive data from the interactive session. | ||||||||||
| 56 | |||||||||||
| 57 | You should not use this class directly, but instead inherit from it in | ||||||||||
| 58 | specific Transport that will set the application command line name, and | ||||||||||
| 59 | marshall any runtime options. The OS platform is detected automatically. | ||||||||||
| 60 | |||||||||||
| 61 | =head1 INTERFACE | ||||||||||
| 62 | |||||||||||
| 63 | =head2 init | ||||||||||
| 64 | |||||||||||
| 65 | This method I<must> be called before any other, to bootstrap the application | ||||||||||
| 66 | wrapper module (IPC::Run or Net::Telnet). However, via L<Net::CLI::Interact>'s | ||||||||||
| 67 | C<cmd>, C<match> or C<find_prompt> it will be called for you automatically. | ||||||||||
| 68 | |||||||||||
| 69 | Two attributes of the specific loaded Transport are used. First the | ||||||||||
| 70 | Application set in C<app> is of course required, plus the options in the | ||||||||||
| 71 | Transport's C<runtime_options> are retrieved, if set, and passed as command | ||||||||||
| 72 | line arguments to the Application. | ||||||||||
| 73 | |||||||||||
| 74 | =head2 connect_ready | ||||||||||
| 75 | |||||||||||
| 76 | Returns True if C<connect> has been called successfully, otherwise returns | ||||||||||
| 77 | False. | ||||||||||
| 78 | |||||||||||
| 79 | =head2 disconnect | ||||||||||
| 80 | |||||||||||
| 81 | Undefines the application wrapper flushes any output data buffer such that | ||||||||||
| 82 | the next call to C<cmd> or C<macro> will cause a new connection to be made. | ||||||||||
| 83 | Useful if you intentionally timeout a command and end up with junk in the | ||||||||||
| 84 | output buffer. | ||||||||||
| 85 | |||||||||||
| 86 | =head2 do_action | ||||||||||
| 87 | |||||||||||
| 88 | When passed a L<Net::CLI::Interact::Action> instance, will execute the | ||||||||||
| 89 | contained instruction on the connected CLI. This might be a command to | ||||||||||
| 90 | C<send>, or a regular expression to C<match> in the output. | ||||||||||
| 91 | |||||||||||
| 92 | Features of the commands and prompts are supported, such as Continuation | ||||||||||
| 93 | matching (and slurping), and sending without an I<output record separator>. | ||||||||||
| 94 | |||||||||||
| 95 | On failing to succeed with a Match, the module will time-out (see C<timeout>, | ||||||||||
| 96 | below) and raise an exception. | ||||||||||
| 97 | |||||||||||
| 98 | Output returned after issuing a command is stored within the Match Action's | ||||||||||
| 99 | C<response> and C<response_stash> slots by this method, with the latter then | ||||||||||
| 100 | marshalled into the correct C<send> Action by the | ||||||||||
| 101 | L<ActionSet|Net::CLI::Interact::ActionSet>. | ||||||||||
| 102 | |||||||||||
| 103 | =head2 put( @data ) | ||||||||||
| 104 | |||||||||||
| 105 | Items in C<@data> are joined together by an empty string and sent as input to | ||||||||||
| 106 | the connected program's interactive session. | ||||||||||
| 107 | |||||||||||
| 108 | =head2 pump | ||||||||||
| 109 | |||||||||||
| 110 | Attempts to retrieve pending output from the connected program's interactive | ||||||||||
| 111 | session. Returns true if there is new data available in the buffer, else | ||||||||||
| 112 | will time-out and raise a Perl exception. See C<buffer> and C<timeout>. | ||||||||||
| 113 | |||||||||||
| 114 | =head2 flush | ||||||||||
| 115 | |||||||||||
| 116 | Empties the buffer used for response data returned from the connected CLI, and | ||||||||||
| 117 | returns that data as a single text string (possibly with embedded newlines). | ||||||||||
| 118 | |||||||||||
| 119 | =head2 timeout( $seconds? ) | ||||||||||
| 120 | |||||||||||
| 121 | When C<do_action> is polling for response data matching a regular expression | ||||||||||
| 122 | Action, it will eventually time-out and throw an exception if nothing matches | ||||||||||
| 123 | and no more data arrives. | ||||||||||
| 124 | |||||||||||
| 125 | The number of seconds to wait is set via this method, which will also return | ||||||||||
| 126 | the current value of C<timeout>. The default value is 10 seconds. | ||||||||||
| 127 | |||||||||||
| 128 | =head2 irs_re | ||||||||||
| 129 | |||||||||||
| 130 | Returns the Regular Expression reference used to split lines of response from | ||||||||||
| 131 | the connected device. In the end, you will only receive data from this module | ||||||||||
| 132 | separated by the C<ors> value (by default a newline character). The C<irs_re> | ||||||||||
| 133 | is used internally by the module and is: | ||||||||||
| 134 | |||||||||||
| 135 | qr/(?:\015\012|\015|\012)/ # i.e. CRLF or CR or LF | ||||||||||
| 136 | |||||||||||
| 137 | =head2 ors | ||||||||||
| 138 | |||||||||||
| 139 | Line separator character(s) appended to a command sent to the connected CLI. | ||||||||||
| 140 | This defaults to a newline on the application's platform. | ||||||||||
| 141 | |||||||||||
| 142 | =head2 logger | ||||||||||
| 143 | |||||||||||
| 144 | Slot for storing a reference to the application's | ||||||||||
| 145 | L<Logger|Net::CLI::Interact::Logger> object. | ||||||||||
| 146 | |||||||||||
| 147 | =head2 is_win32 | ||||||||||
| 148 | |||||||||||
| 149 | Returns true if the current platform is Windows. Can be called as either a | ||||||||||
| 150 | class or instance method. | ||||||||||
| 151 | |||||||||||
| 152 | =head2 app | ||||||||||
| 153 | |||||||||||
| 154 | Location and name of the program used to establish an interactive CLI session. | ||||||||||
| 155 | On Unix platforms this will be C<ssh> (openssh), C<telnet>, or C<cu> (serial | ||||||||||
| 156 | line). On Windows this must be the C<plink.exe> program. | ||||||||||
| 157 | |||||||||||
| 158 | =head2 connect_options | ||||||||||
| 159 | |||||||||||
| 160 | Slot for storing a set of options for the specific loaded Transport, passed by | ||||||||||
| 161 | the user of Net::CLI::Interact as a hash ref. Do not access this directly, but | ||||||||||
| 162 | instead use C<runtime_options> from the specific Transport class. | ||||||||||
| 163 | |||||||||||
| 164 | =head2 wrapper | ||||||||||
| 165 | |||||||||||
| 166 | Slot for storing the application wrapper instance (IPC::Run or Net::Telnet). | ||||||||||
| 167 | Do not mess with this unless you know what you are doing. | ||||||||||
| 168 | |||||||||||
| 169 | =head2 buffer | ||||||||||
| 170 | |||||||||||
| 171 | After C<pump> returns successfully, the output most recently received is | ||||||||||
| 172 | stored in this slot. Do not access this directly, but instead use the C<flush> | ||||||||||
| 173 | method. | ||||||||||
| 174 | |||||||||||
| 175 | =head2 stash | ||||||||||
| 176 | |||||||||||
| 177 | During long sections of output, this slot allows more efficient detection of | ||||||||||
| 178 | matches. Older data is placed here, and only the most recent line of data is | ||||||||||
| 179 | stored in the C<buffer>. That's why C<flush> is the only way to ensure you get | ||||||||||
| 180 | all the output data in one go. | ||||||||||
| 181 | |||||||||||
| 182 | =head1 NOTES | ||||||||||
| 183 | |||||||||||
| 184 | B<FIXME>: On Unix, when the Telnet transport is selected but C<IP::Pty> is | ||||||||||
| 185 | unavailable, C<Net::Telnet> can still be used, but currently C<IPC::Run> is | ||||||||||
| 186 | used instead. | ||||||||||
| 187 | |||||||||||
| 188 | =head1 AUTHOR | ||||||||||
| 189 | |||||||||||
| 190 | Oliver Gorwits <oliver@cpan.org> | ||||||||||
| 191 | |||||||||||
| 192 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 193 | |||||||||||
| 194 | This software is copyright (c) 2014 by Oliver Gorwits. | ||||||||||
| 195 | |||||||||||
| 196 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 197 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 198 | |||||||||||
| 199 | =cut | ||||||||||
| 200 | |||||||||||