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