« blib/lib/Net/Appliance/Session/Scripting.pm
↑ Summary
blib/lib/Net/Appliance/Session/Transport.pm »
| 1 | package Net::Appliance::Session::Engine; | ||||||||||||||||||||||||||||
| 2 | 1 | { $Net::Appliance::Session::Engine::VERSION = '4.300004' } | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Sub::Quote; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MooX::Types::MooseLike::Base qw(Int); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | has 'pager_enable_lines' => ( | |||||||||||||||||||||||||||
| 9 | is => 'rw', | ||||||||||||||||||||||||||||
| 10 | isa => Int, | ||||||||||||||||||||||||||||
| 11 | required => 0, | ||||||||||||||||||||||||||||
| 12 | default => quote_sub('24'), | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | has 'pager_disable_lines' => ( | |||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | isa => Int, | ||||||||||||||||||||||||||||
| 18 | required => 0, | ||||||||||||||||||||||||||||
| 19 | default => quote_sub('0'), | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub enable_paging { | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
enable_paging: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return unless $self->do_paging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return unless $self->logged_in; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | my $privstate = $self->in_privileged_mode; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | $self->begin_privileged if $self->privileged_paging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $pagercmd = ($self->nci->phrasebook->has_macro('enable_paging') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | ? 'enable_paging' : 'paging'); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | $self->macro($pagercmd, { params => [ | |||||||||||||||||||||||||||
| 35 | $self->pager_enable_lines | ||||||||||||||||||||||||||||
| 36 | ]} ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $self->end_privileged | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->privileged_paging and not $privstate
MC/DC0%0/2
$self->privileged_paging and not $privstate
$self->privileged_paging
not $privstate
Truth table0%0/3
A and not(B)
A= $self->privileged_pagingB= not $privstate
| |||||||||||||||||||||||||||||
| 39 | if $self->privileged_paging and not $privstate; | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub disable_paging { | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
disable_paging: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return unless $self->do_paging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return unless $self->logged_in; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $privstate = $self->in_privileged_mode; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | $self->begin_privileged if $self->privileged_paging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $pagercmd = ($self->nci->phrasebook->has_macro('disable_paging') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | ? 'disable_paging' : 'paging'); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $self->macro($pagercmd, { params => [ | |||||||||||||||||||||||||||
| 55 | $self->pager_disable_lines | ||||||||||||||||||||||||||||
| 56 | ]} ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $self->end_privileged | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->privileged_paging and not $privstate
MC/DC0%0/2
$self->privileged_paging and not $privstate
$self->privileged_paging
not $privstate
Truth table0%0/3
A and not(B)
A= $self->privileged_pagingB= not $privstate
| |||||||||||||||||||||||||||||
| 59 | if $self->privileged_paging and not $privstate; | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # method to enter privileged mode on the remote device. | ||||||||||||||||||||||||||||
| 63 | # optionally, use a different username and password to those | ||||||||||||||||||||||||||||
| 64 | # used at login time. if using a different username then we'll | ||||||||||||||||||||||||||||
| 65 | # explicily login rather than privileged. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub begin_privileged { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
begin_privileged: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 0 | my $options = Net::Appliance::Session::Transport::ConnectOptions->new(@_); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return unless $self->do_privileged_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return if $self->in_privileged_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | die 'must connect before you can begin_privileged' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | unless $self->logged_in; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # rt.cpan#47214 check if we are already enabled by peeking the prompt | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($self->prompt_looks_like('privileged')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | $self->in_privileged_mode(1); | |||||||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # default is to re-use login credentials | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $username = $options->has_username ? $options->username : $self->get_username; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # rt.cpan#69139 support passing of privileged_password to the constructor | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my $password = $options->has_password ? $options->password : | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | $self->has_privileged_password ? $self->get_privileged_password | ||||||||||||||||||||||||||||
| 89 | : $self->get_password; | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | $self->macro('begin_privileged'); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # whether login or enable, we still must be prepared for username | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ($self->prompt_looks_like('user')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | die 'a set username is required to enter priv on this host' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | if not $username; | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $self->cmd($username, { match => 'pass' }); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($self->prompt_looks_like('pass')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | die 'a set password is required before begin_privileged' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | if not $password; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # rt.cpan#92376 timeout when incorrect password | ||||||||||||||||||||||||||||
| 106 | 0 | $self->cmd($password, { match => 'generic' }); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $self->prompt_looks_like('privileged') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | or die 'should be in privileged mode but prompt does not match'; | ||||||||||||||||||||||||||||
| 111 | 0 | $self->in_privileged_mode(1); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub end_privileged { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
end_privileged: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return unless $self->do_privileged_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return unless $self->in_privileged_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | die 'must leave configure mode before leaving privileged mode' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | if $self->in_configure_mode; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $self->macro('end_privileged'); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | not $self->prompt_looks_like('privileged') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | or die 'should have left privileged mode but prompt still matches'; | ||||||||||||||||||||||||||||
| 127 | 0 | $self->in_privileged_mode(0); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub begin_configure { | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
begin_configure: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | return unless $self->do_configure_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return if $self->in_configure_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | die 'must enter privileged mode before configure mode' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | unless $self->in_privileged_mode; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # rt.cpan#47214 check if we are already in config by peeking the prompt | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($self->prompt_looks_like('configure')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $self->in_configure_mode(1); | |||||||||||||||||||||||||||
| 142 | 0 | return; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | $self->macro('begin_configure'); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $self->prompt_looks_like('configure') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | or die 'should be in configure mode but prompt does not match'; | ||||||||||||||||||||||||||||
| 149 | 0 | $self->in_configure_mode(1); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub end_configure { | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
end_configure: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | return unless $self->do_configure_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return unless $self->in_configure_mode; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $self->macro('end_configure'); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # we didn't manage to escape configure mode (must be nested?) | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($self->prompt_looks_like('configure')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | my $caller3 = (caller(3))[3]; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # max out at three tries to exit configure mode | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ( $caller3 and $caller3 =~ m/end_configure$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$caller3 and $caller3 =~ /end_configure$/
MC/DC0%0/2
$caller3 and $caller3 =~ /end_configure$/
$caller3
$caller3 =~ /end_configure$/
Truth table0%0/3
A and B
A= $caller3B= $caller3 =~ /end_configure$/
| |||||||||||||||||||||||||||||
| 166 | 0 | die 'failed to leave configure mode'; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | # try again to exit configure mode | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 0 | $self->end_configure; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # return if recursively called | ||||||||||||||||||||||||||||
| 175 | 0 | my $caller1 = (caller(1))[3]; | |||||||||||||||||||||||||||
| 176 | 0 | ▶ | if ( defined $caller1 and $caller1 =~ m/end_configure$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $caller1 and $caller1 =~ /end_configure$/
MC/DC0%0/2
defined $caller1 and $caller1 =~ /end_configure$/
defined $caller1
$caller1 =~ /end_configure$/
Truth table0%0/3
A and B
A= defined $caller1B= $caller1 =~ /end_configure$/
| |||||||||||||||||||||||||||||
| 177 | 0 | return; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | not $self->prompt_looks_like('configure') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or die 'should have exited configure mode but prompt still matches'; | ||||||||||||||||||||||||||||
| 182 | 0 | $self->in_configure_mode(0); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | 1; | |||||||||||||||||||||||||||