| 1 | package Mnet::Expect; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Mnet::Expect - Create Expect objects with Mnet::Log support | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # refer to SEE ALSO section for other Mnet::Expect modules | ||||||||||||||||||||||||||||
| 10 | use Mnet::Expect; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # spawn Expect telnet process connected to specified host | ||||||||||||||||||||||||||||
| 13 | my $expect = Mnet::Expect->new({ spawn => "telnet 1.2.3.4" }); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # access underlying Expect methods, refer to perldoc Expect | ||||||||||||||||||||||||||||
| 16 | $expect->expect->send("ls\r"); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # gracefully close spawned Expect process | ||||||||||||||||||||||||||||
| 19 | $expect->close; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Mnet::Expect can be used to spawn L<Expect> processes, which can be used | ||||||||||||||||||||||||||||
| 24 | to programmatically control interactive terminal sessions, with support for | ||||||||||||||||||||||||||||
| 25 | L<Mnet> options and logging. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Refer to the perl L<Expect> module for more information. Also refer to the | ||||||||||||||||||||||||||||
| 28 | L<Mnet::Expect::Cli> and L<Mnet::Expect::Cli::Ios> modules, both of which | ||||||||||||||||||||||||||||
| 29 | build on this module. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Mnet::Expect implements the methods listed below. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # required modules | ||||||||||||||||||||||||||||
| 38 | # importing Expect namespace conflicted with expect sub here | ||||||||||||||||||||||||||||
| 39 | 39 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 39 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 39 | ▶ | use parent qw( Mnet::Log::Conditional ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 39 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 39 | ▶ | use Errno; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 39 | ▶ | use Expect(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 39 | ▶ | use Mnet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 39 | ▶ | use Mnet::Dump; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 39 | ▶ | use Mnet::Opts::Cli::Cache; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 39 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 new | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | $expect = Mnet::Expect->new(\%opts) | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | This method can be used to create new Mnet::Expect objects. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The following input opts may be specified: | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | log_expect default session debug, refer to log_expect method | ||||||||||||||||||||||||||||
| 62 | log_id refer to perldoc Mnet::Log new method | ||||||||||||||||||||||||||||
| 63 | raw_pty undef, can be set 0 or 1, refer to perldoc Expect | ||||||||||||||||||||||||||||
| 64 | spawn command and args array ref, or space separated string | ||||||||||||||||||||||||||||
| 65 | winsize specify session rows and columns, default 99999x999 | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | An error is issued if there are spawn problems. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | For example, the following will spawn an telnet expect session to a device: | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | my $expect = Mnet::Expect->new({ spawn => "telnet 1.2.3.4" }); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Note that all connected session activity is logged for debugging, refer to | ||||||||||||||||||||||||||||
| 74 | the L<Mnet::Log> module for more information. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # read input class and options hash ref merged with cli options | ||||||||||||||||||||||||||||
| 79 | 39 | ▶ | my $class = shift // croak("missing class arg"); | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // croak("missing class arg")
MC/DC0%0/1
shift() // croak("missing class arg")
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 80 | 39 | ▶ | my $opts = Mnet::Opts::Cli::Cache::get(shift // {}); | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // {}
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # create log object with input opts hash, cli opts, and pragmas in effect | ||||||||||||||||||||||||||||
| 83 | # ensures we can log correctly even if inherited object creation fails | ||||||||||||||||||||||||||||
| 84 | 39 | my $log = Mnet::Log::Conditional->new($opts); | |||||||||||||||||||||||||||
| 85 | 39 | $log->debug("new starting"); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # create hash that will become new object from input opts hash | ||||||||||||||||||||||||||||
| 88 | 39 | my $self = $opts; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # note default options for this class | ||||||||||||||||||||||||||||
| 91 | # includes recognized input opts and cli opts for this object | ||||||||||||||||||||||||||||
| 92 | # the following keys starting with underscore are used internally: | ||||||||||||||||||||||||||||
| 93 | # _expect => spawned Expect object, refer to Mnet::Expect->expect | ||||||||||||||||||||||||||||
| 94 | # _log_filter => set to password text to filter from debug log once | ||||||||||||||||||||||||||||
| 95 | # _no_spawn => set true to skip spawn, used by sub-modules' replay | ||||||||||||||||||||||||||||
| 96 | # in addition refer to perldoc for input opts and Mnet::Log0->new opts | ||||||||||||||||||||||||||||
| 97 | # update perldoc for this sub with changes | ||||||||||||||||||||||||||||
| 98 | my $defaults = { | ||||||||||||||||||||||||||||
| 99 | debug => $opts->{debug}, | ||||||||||||||||||||||||||||
| 100 | _expect => undef, | ||||||||||||||||||||||||||||
| 101 | _log_filter => undef, | ||||||||||||||||||||||||||||
| 102 | log_id => $opts->{log_id}, | ||||||||||||||||||||||||||||
| 103 | log_expect => "debug", | ||||||||||||||||||||||||||||
| 104 | _no_spawn => undef, | ||||||||||||||||||||||||||||
| 105 | quiet => $opts->{quiet}, | ||||||||||||||||||||||||||||
| 106 | raw_pty => undef, | ||||||||||||||||||||||||||||
| 107 | silent => $opts->{silent}, | ||||||||||||||||||||||||||||
| 108 | 39 | spawn => undef, | |||||||||||||||||||||||||||
| 109 | winsize => "99999x999", | ||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # update future object $self hash with default opts | ||||||||||||||||||||||||||||
| 113 | 39 | foreach my $opt (sort keys %$defaults) { | |||||||||||||||||||||||||||
| 114 | 429 | ▶ | $self->{$opt} = $defaults->{$opt} if not exists $self->{$opt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # debug opts set here, hide internal opts starting w/underscore | ||||||||||||||||||||||||||||
| 118 | 39 | foreach my $opt (sort keys %$self) { | |||||||||||||||||||||||||||
| 119 | 1183 | ▶ | if ($opt !~ /^_/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 998 | my $value = Mnet::Dump::line($self->{$opt}); | |||||||||||||||||||||||||||
| 121 | 998 | $log->debug("new opt $opt = $value"); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # bless new object | ||||||||||||||||||||||||||||
| 126 | 39 | bless $self, $class; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # call to log_expect to ensure that log_level is valid | ||||||||||||||||||||||||||||
| 129 | 39 | $self->log_expect($self->{log_expect}); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # return undef if Expect spawn does not succeed | ||||||||||||||||||||||||||||
| 132 | 38 | $self->debug("new calling spawn"); | |||||||||||||||||||||||||||
| 133 | 38 | ▶ | if (not $self->spawn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $self->debug("new finished, spawn failed, returning undef"); | |||||||||||||||||||||||||||
| 135 | 0 | return undef; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # finished new method, return Mnet::Expect object | ||||||||||||||||||||||||||||
| 139 | 37 | $self->debug("new finished, returning $self"); | |||||||||||||||||||||||||||
| 140 | 37 | return $self; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 38 | ▶ | sub spawn { | ||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=12 SCAR=25.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # $ok = $self->spawn | ||||||||||||||||||||||||||||
| 148 | # purpose: used to spawn Expect object | ||||||||||||||||||||||||||||
| 149 | # $ok: set true on success, false on failure | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # read input object | ||||||||||||||||||||||||||||
| 152 | 38 | my $self = shift; | |||||||||||||||||||||||||||
| 153 | 38 | $self->debug("spawn starting"); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # return true if _no_spawn is set | ||||||||||||||||||||||||||||
| 156 | # this is used for replay from sub-modules | ||||||||||||||||||||||||||||
| 157 | # this avoids interference with replay in the module | ||||||||||||||||||||||||||||
| 158 | 38 | ▶ | if ($self->{_no_spawn}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 3 | $self->debug("spawn skipped for _no_spawn"); | |||||||||||||||||||||||||||
| 160 | 3 | return 1; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # error if spawn option was not set | ||||||||||||||||||||||||||||
| 164 | 35 | ▶ | croak("missing spawn option") if not defined $self->{spawn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # conditionally load perl Expect module and create new expect object | ||||||||||||||||||||||||||||
| 167 | # we are only loading the Expect module if this method is called | ||||||||||||||||||||||||||||
| 168 | # require is used so as to not import anything into this namespace | ||||||||||||||||||||||||||||
| 169 | 35 | ▶ | eval("require Expect; 1") or croak("missing Expect perl module"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 35 | $self->{_expect} = Expect->new; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # set raw_pty for expect session if defined as an input option | ||||||||||||||||||||||||||||
| 173 | 35 | ▶ | $self->{_expect}->raw_pty($self->{raw_pty}) if defined $self->{raw_pty}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # set default window size for expect tty session | ||||||||||||||||||||||||||||
| 176 | # this defaults to a large value to minimize pagination and line wrapping | ||||||||||||||||||||||||||||
| 177 | # IO::Tty::Constant module is pulled into namespace when Expect is used | ||||||||||||||||||||||||||||
| 178 | croak("bad winsize $self->{winsize}") | ||||||||||||||||||||||||||||
| 179 | 35 | ▶ | if $self->{winsize} !~ /^(\d+)x(\d+)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 35 | my $tiocswinsz = IO::Tty::Constant::TIOCSWINSZ(); | |||||||||||||||||||||||||||
| 181 | 35 | my $winsize_pack = pack('SSSS', $1, $2, 0, 0); | |||||||||||||||||||||||||||
| 182 | 35 | ioctl($self->expect->slave, $tiocswinsz, $winsize_pack); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # set Mnet::Expect->log method for logging | ||||||||||||||||||||||||||||
| 185 | # disable expect stdout logging | ||||||||||||||||||||||||||||
| 186 | 35 | $self->expect->log_stdout(0); | |||||||||||||||||||||||||||
| 187 | 35 | ▶ | $self->expect->log_file(sub { $self->_log(shift) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # note spawn command and arg list | ||||||||||||||||||||||||||||
| 190 | # this can be specified as a list reference or a space-separated string | ||||||||||||||||||||||||||||
| 191 | 35 | my @spawn = (); | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | @spawn = @{$self->{spawn}} if ref $self->{spawn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 35 | ▶ | @spawn = split(/\s/, $self->{spawn}) if not ref $self->{spawn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 35 | $self->debug("spawn arg: $_") foreach @spawn; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # call Expect spawn method | ||||||||||||||||||||||||||||
| 197 | # disable Mnet::Tee stdout/stderr ties if not Mnet::Tee is loaded | ||||||||||||||||||||||||||||
| 198 | # stdout/stderr ties cause spawn problems, but can be re-enabled after | ||||||||||||||||||||||||||||
| 199 | # init global Mnet::Expect error to undef, set on expect spawn failures | ||||||||||||||||||||||||||||
| 200 | 35 | ▶ | if ($INC{'Mnet/Tee.pm'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 23 | Mnet::Tee::tie_disable(); | |||||||||||||||||||||||||||
| 202 | 23 | $self->debug("spawn calling Expect module spawm method"); | |||||||||||||||||||||||||||
| 203 | 23 | ▶ | $self->fatal("spawn error, $!") if not $self->expect->spawn(@spawn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 23 | Mnet::Tee::tie_enable(); | |||||||||||||||||||||||||||
| 205 | } else { | ||||||||||||||||||||||||||||
| 206 | 12 | ▶ | $self->fatal("spawn error, $!") if not $self->expect->spawn(@spawn); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # note spawn process id | ||||||||||||||||||||||||||||
| 210 | 34 | $self->debug("spawn pid ".$self->expect->pid); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # finished spawn method, return true for success | ||||||||||||||||||||||||||||
| 213 | 34 | $self->debug("spawn finished, returning true"); | |||||||||||||||||||||||||||
| 214 | 34 | return 1; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 17 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=5 SCAR=17.4
Pod
documented
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head2 close | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | $expect->close | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Attempt to call hard_close for the current Mnet::Expect objects L<Expect> | ||||||||||||||||||||||||||||
| 226 | session, and send a kill signal if the process still exists. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | The L<Expect> object associated with the current Mnet::Expect object will be | ||||||||||||||||||||||||||||
| 229 | set to undefined. Refer also to the expect method documented below. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # read input object | ||||||||||||||||||||||||||||
| 234 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 235 | 17 | $self->debug("close starting"); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # return if expect object no longer defined | ||||||||||||||||||||||||||||
| 238 | 17 | ▶ | if (not defined $self->expect) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 1 | $self->debug("close finished, expect not defined"); | |||||||||||||||||||||||||||
| 240 | 1 | return; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # note process id of spawned expect command | ||||||||||||||||||||||||||||
| 244 | 16 | my $spawned_pid = $self->expect->pid; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # return if there's no expect process id | ||||||||||||||||||||||||||||
| 247 | 16 | ▶ | if (not defined $spawned_pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 8 | $self->debug("close finished, no expect pid"); | |||||||||||||||||||||||||||
| 249 | 8 | $self->{_expect} = undef; | |||||||||||||||||||||||||||
| 250 | 8 | return; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # continue processing | ||||||||||||||||||||||||||||
| 254 | 8 | $self->debug("close proceeding for pid $spawned_pid"); | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # usage: $result = _close_confirmed($self, $label, $spawned_pid) | ||||||||||||||||||||||||||||
| 257 | # kill(0,$pid) is true if pid signalable, Errno::ESRCH if not found | ||||||||||||||||||||||||||||
| 258 | # purpose: return true if $spawned_pid is gone, $label used for debug | ||||||||||||||||||||||||||||
| 259 | # note: if result is true then expect object will have been set undefined | ||||||||||||||||||||||||||||
| 260 | 8 | ▶ | sub _close_confirmed { | ||||||||||||||||||||||||||
|
Subroutine
_close_confirmed: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 261 | 8 | my ($self, $label, $spawned_pid) = (shift, shift, shift); | |||||||||||||||||||||||||||
| 262 | 8 | ▶ | if (not kill(0, $spawned_pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 8 | ▶ | if ($! == Errno::ESRCH) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 8 | $self->debug("close finished, $label confirmed"); | |||||||||||||||||||||||||||
| 265 | 8 | $self->{_expect} = undef; | |||||||||||||||||||||||||||
| 266 | 8 | return 1; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | 0 | $self->debug("close pid check error after $label, $!"); | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | return 0; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # call hard close | ||||||||||||||||||||||||||||
| 274 | # ignore int and term signals to avoid hung processes | ||||||||||||||||||||||||||||
| 275 | 8 | $self->debug("close calling hard_close"); | |||||||||||||||||||||||||||
| 276 | 8 | eval { | |||||||||||||||||||||||||||
| 277 | 8 | local $SIG{INT} = "IGNORE"; | |||||||||||||||||||||||||||
| 278 | 8 | local $SIG{TERM} = "IGNORE"; | |||||||||||||||||||||||||||
| 279 | 8 | $self->expect->hard_close; | |||||||||||||||||||||||||||
| 280 | }; | ||||||||||||||||||||||||||||
| 281 | 8 | ▶ | return if _close_confirmed($self, "hard_close", $spawned_pid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # if hard_close failed then send kill -9 signal | ||||||||||||||||||||||||||||
| 284 | 0 | $self->debug("close sending kill signal"); | |||||||||||||||||||||||||||
| 285 | 0 | kill(9, $spawned_pid); | |||||||||||||||||||||||||||
| 286 | 0 | ▶ | return if _close_confirmed($self, "kill", $spawned_pid); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # undefine expect object since nothing else worked | ||||||||||||||||||||||||||||
| 289 | 0 | $self->{_expect} = undef; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # finished close method | ||||||||||||||||||||||||||||
| 292 | 0 | $self->debug("close finished, expect undef after kill"); | |||||||||||||||||||||||||||
| 293 | 0 | return; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 689 | ▶ | sub expect { | ||||||||||||||||||||||||||
|
Subroutine
expect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head2 expect | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | $expect->expect | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | Returns the underlying expect object used by this module, for access to fetures | ||||||||||||||||||||||||||||
| 305 | that may not be supported directly by Mnet::Expect modules. Refer to the | ||||||||||||||||||||||||||||
| 306 | L<Expect> module for more information. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =cut | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # return underlying expect object | ||||||||||||||||||||||||||||
| 311 | 689 | my $self = shift; | |||||||||||||||||||||||||||
| 312 | 689 | return $self->{_expect}; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 131 | ▶ | sub _log { | ||||||||||||||||||||||||||
|
Subroutine
_log: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # $self->_log($chars) | ||||||||||||||||||||||||||||
| 320 | # purpose: output Mnet::Expect session activity, as per log_expect | ||||||||||||||||||||||||||||
| 321 | # $chars: logged text, non-printable characters are output as hexadecimal | ||||||||||||||||||||||||||||
| 322 | # note: Mnet::Expect->spawn sets Expect log_file to use this method | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | # read the current Mnet::Expect object and character string to log | ||||||||||||||||||||||||||||
| 325 | 131 | my ($self, $chars) = (shift, shift); | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | # init text and hex log output lines | ||||||||||||||||||||||||||||
| 328 | # separate hex lines are used to show non-prinatbel characters | ||||||||||||||||||||||||||||
| 329 | 131 | my ($line_txt, $line_hex) = (undef, undef); | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | # note log level for expect session traffic, using log_expect | ||||||||||||||||||||||||||||
| 332 | 131 | my $log_expect = $self->{log_expect}; | |||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # return if log_expect was set undef to disable logging | ||||||||||||||||||||||||||||
| 335 | # this might come in handy to ensure secrets don't end up in logs | ||||||||||||||||||||||||||||
| 336 | 131 | ▶ | return if not defined $log_expect; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # loop through input hex and text characters | ||||||||||||||||||||||||||||
| 339 | 131 | foreach my $char (split(//, $chars)) { | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # append non-printable ascii characters to line_hex | ||||||||||||||||||||||||||||
| 342 | # apply then clear _log_filter to remove passwords from line_txt | ||||||||||||||||||||||||||||
| 343 | 1325 | ▶ | if (ord($char) < 32) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 204 | $line_hex .= sprintf(" %02x", ord($char)); | |||||||||||||||||||||||||||
| 345 | 204 | ▶ | if (defined $line_txt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 70 | ▶ | if (defined $self->{_log_filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 4 | ▶ | if ($line_txt =~ s/\Q$self->{_log_filter}\E/****/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 4 | $self->{_log_filter} = undef; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | 70 | $self->$log_expect("log txt: $line_txt"); | |||||||||||||||||||||||||||
| 352 | 70 | $line_txt = undef; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | # append printable ascii characters to line_txt, output log hex | ||||||||||||||||||||||||||||
| 356 | # log hex always goes to debug | ||||||||||||||||||||||||||||
| 357 | } else { | ||||||||||||||||||||||||||||
| 358 | 1121 | $line_txt .= $char; | |||||||||||||||||||||||||||
| 359 | 1121 | ▶ | if (defined $line_hex) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 69 | $self->debug("log hex:$line_hex"); | |||||||||||||||||||||||||||
| 361 | 69 | $line_hex = undef; | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | # continue looping through input characters | ||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # output any remaining log hex and txt lines after finishing loop | ||||||||||||||||||||||||||||
| 369 | # log hex always goes to debug, log txt controlled by log_expct value | ||||||||||||||||||||||||||||
| 370 | # apply and clear _log_filter to remove passwords from line_txt | ||||||||||||||||||||||||||||
| 371 | 131 | ▶ | $self->debug("log hex:$line_hex") if defined $line_hex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 131 | ▶ | if (defined $line_txt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | 98 | ▶ | if (defined $self->{_log_filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 3 | ▶ | if ($line_txt =~ s/\Q$self->{_log_filter}\E/****/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 1 | $self->{_log_filter} = undef; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | 98 | $self->$log_expect("log txt: $line_txt"); | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # finished _log method | ||||||||||||||||||||||||||||
| 382 | 131 | return; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 41 | ▶ | sub log_expect { | ||||||||||||||||||||||||||
|
Subroutine
log_expect: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =head1 log_expect | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | $prior = $expect->log_expect($level) | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | Use this method to set a new log_expect level for data received in the expect | ||||||||||||||||||||||||||||
| 394 | session. The prior log_expect value will be returned. | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | The new log_expect level can be set to debug, info, or undefined. An undefined | ||||||||||||||||||||||||||||
| 397 | log_expect disables the logging of expect session traffic, which might be | ||||||||||||||||||||||||||||
| 398 | useful to keep sensitive data out of log outputs. | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | The default log level for expect session traffic is debug. | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | An invalid input log_expect level results in an error. | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =cut | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | # read the current Mnet::Expect object and new log level | ||||||||||||||||||||||||||||
| 407 | 41 | my ($self, $level) = (shift, shift); | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | # abort with an error if input log_expect level is not valid | ||||||||||||||||||||||||||||
| 410 | 41 | ▶ | croak("log_expect invalid, must be debug, info, or undef") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $level and not $level =~ /^(debug|info)$/
MC/DC50%1/2
defined $level and not $level =~ /^(debug|info)$/
defined $level
not $level =~ /^(debug|info)$/
Truth table66%2/3
A and not(B)
A= defined $levelB= not $level =~ /^(debug|info)$/
| |||||||||||||||||||||||||||||
| 411 | if defined $level and $level !~ /^(debug|info)$/; | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | # note prior log_expect value and set the new value | ||||||||||||||||||||||||||||
| 414 | 40 | my $prior_log_expect = $self->{log_expect}; | |||||||||||||||||||||||||||
| 415 | 40 | $self->{log_expect} = $level; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | # finished log_expect method, return prior_log_expct value | ||||||||||||||||||||||||||||
| 418 | 40 | return $prior_log_expect; | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =head1 TESTING | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Mnet::Expect does not include iteself include support for L<Mnet::Test> | ||||||||||||||||||||||||||||
| 426 | functionality. This is a low level module that spawns expect sessions but does | ||||||||||||||||||||||||||||
| 427 | not know how to talk to devices. Any desired test functionality would need to | ||||||||||||||||||||||||||||
| 428 | be provided by the calling script. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | L<Expect> | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | L<Mnet> | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | L<Mnet::Expect::Cli> | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | L<Mnet::Expect::Cli::Ios> | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | L<Mnet::Log> | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | L<Mnet::Opts::Cli> | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | L<Mnet::Test> | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =cut | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | # normal package return | ||||||||||||||||||||||||||||
| 449 | 39 | 1; | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||