blib/lib/RAS/AS5200.pm

filter: Statementstmt 10.6% 17 / 159 Branchbran 1.6% 1 / 60 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 36.3% 4 / 11 Podpod 100% 8 / 8 totaltotal 11.7% 31 / 263 CC 43 SCAR 43.9
CC
43
Cov
8%
run_command
210.0
usergrep
56.0
userkill
56.0
CRAP 80.6
↑ Summary
1 ### AS5200.pm
2 ### PERL module for talking to a Cisco AS5200 access router
3 #########################################################
4  
5 package RAS::AS5200;
6 1 $VERSION = "1.04";
7  
8 1 ▶use strict "subs"; use strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # This uses Net::Telnet to connect to the RAS
11 1 ▶use Net::Telnet ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # The name $ras will be used consistently as the
14 # reference to the RAS::AS5200 object we're handling
15  
16 # The constructor method, of course
17 1 ▶sub new {
Subroutine new: called CC=5 SCAR=22.9
Pod documented
18 1 my($class) = shift ;
19 1 my($ras) = {} ;
20 1 %$ras = @_ ;
21  
22 1 ▶ unless ($ras->{hostname}) { warn "ERROR: ", (ref($class) || $class), " - Hostname not specified.\n"; return(); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ras->{"hostname"}) 1 0 1
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
23 0 $ras->{'VERSION'} = $VERSION;
24 0 ▶ $ras->{prompt} = '\w*[>#]' unless $ras->{prompt};
Branch: 1 branch
BranchTrueFalseTotal
unless $ras->{"prompt"} 0 0 0
25  
26 0 ▶ bless($ras, ref($class) || $class);
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
27 }
28  
29  
30 # for debugging - printenv() prints to STDERR
31 # the entire contents of %$ras
32 0 ▶sub printenv {
Subroutine printenv: not called CC=2 SCAR=17.9
Pod documented
33 0 my($ras) = shift;
34 0 ▶ while (($key,$value) = each(%$ras)) { warn "$key = $value\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if (($key, $value) = each %$ras) 0 0 0
35 0 return();
36 }
37  
38  
39 # run_command() is the heart of this module's functionality
40 # This runs the specified commands on the router and returns
41 # a list of refs to arrays containing the commands' output
42 0 ▶sub run_command {
Subroutine run_command: not called CC=14 SCAR=53.5
Pod documented
43 0 my($ras) = shift;
44 0 my(@returnlist);
45 0 my($prompt) = '/' . $ras->{prompt} . '$/';
46  
47 0 ▶ while ($command = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command = shift()) 0 0 0
48 0 my($session) = new Net::Telnet;
49 0 $session->errmode("return");
50 0 $session->open($ras->{hostname});
51 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
52 0 warn "ERROR: ",ref($ras)," - Cannot connect to host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
53  
54 # If a login name is supplied, wait for the Login: prompt.
55 # Otherwise, send a blank password.
56 # This is a workaround for 2 changes in early versions of IOS 11
57 # 1. IOS 11.3 asks for login and password, 11.2 asks only for password
58 # 2. IOS 11.2 - the first password prompt fails to authenticate about
59 # 1/4 of the time, so it's best to send a blank password for the
60 # first password prompt
61 0 ▶ if ($ras->{login}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ras->{'login'}) { } 0 0 0
62 0 $session->waitfor('/Login: $/i');
63 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
64 0 warn "ERROR: ",ref($ras)," - Waiting for login prompt on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
65 0 $session->print($ras->{login});
66 }
67 else {
68 0 $session->waitfor('/Password: $/i');
69 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
70 0 warn "ERROR: ",ref($ras)," - Waiting for password prompt on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
71 0 $session->print("");
72 }
73 0 $session->waitfor('/Password: $/i');
74 0 $session->print($ras->{password});
75 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
76 0 warn "ERROR: ",ref($ras)," - Waiting for password prompt on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
77  
78 # Okay, we're logged in. Get the command prompt.
79 0 $session->waitfor($prompt);
80 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
81 0 warn "ERROR: ",ref($ras)," - Waiting for command prompt on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
82 0 my(@output);
83  
84 # If the command was prefixed with "ENABLE " then go into enable mode first.
85 0 ▶ if ($command =~ s/^ENABLE //) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ s/^ENABLE //) 0 0 0
86 0 $session->print("enable");
87 0 $session->waitfor('/Password: $/');
88 0 $session->print($ras->{enablepassword});
89 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
90 0 warn "ERROR: ",ref($ras)," - Waiting for enable password prompt on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
91 0 $session->waitfor($prompt);
92 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
93 0 warn "ERROR: ",ref($ras)," - Waiting for post-enable command prompt on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
94 }
95  
96 # Send the command and keep paging down and grab the output
97 0 $session->print($command);
98 0 while (1) {
99 0 $session->print(""); my($line) = $session->getline;
100 0 ▶ if ($session->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->errmsg) 0 0 0
101 0 warn "ERROR: ",ref($ras)," - Waiting on output from command \"$command\" on host $ras->{hostname} - ",$session->errmsg,"\n"; return(); }
102 0 ▶ if ($line eq "[confirm]") { $session->print("y"); next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "[confirm]") 0 0 0
103 0 ▶ if ($line =~ /^$ras->{prompt}$/) { $session->print("exit"); $session->close; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^$ras->{"prompt"}$/) 0 0 0
104 0 $line =~ s/^\s?--More--\s*\010+\s+\010+//;
105 0 push(@output, $line);
106 }
107  
108 0 shift(@output); # Trim the echoed command
109 0 push(@returnlist, \@output);
110 } # end of shifting commands
111  
112 # We're returning a list of references to lists.
113 # Each ref points to an array containing the returned text
114 # from the command, and the list of refs corresponds
115 # to the list of commands we were given
116 0 return(@returnlist);
117 } # end of run_command
118  
119  
120 # usergrep() - takes a username and returns an array of
121 # ports on which the user was found
122 0 ▶sub usergrep {
Subroutine usergrep: not called CC=7 SCAR=40.3
Pod documented
123 0 my($ras) = shift;
124 0 ▶ my($username) = shift; return() unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
125 0 ▶ if ($ras->{truncateusernames}) { $username = substr($username,0,10); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ras->{"truncateusernames"}) 0 0 0
126 0 my($output) = $ras->run_command('show users');
127 0 my(@ports);
128  
129 0 foreach (@$output) {
130 0 my($port,$user);
131 0 ▶ next unless (/^\s+\d+ tty \d+\s/ || /^\s+Se\d+\:\d+\s/);
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s+\d+ tty \d+\s/ or /^\s+Se\d+\:\d+\s/ 0 0 0
Condition0%0/3
/^\s+\d+ tty \d+\s/ or /^\s+Se\d+\:\d+\s/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\s+\d+ tty \d+\s/ or /^\s+Se\d+\:\d+\s/
/^\s+\d+ tty \d+\s/ /^\s+Se\d+\:\d+\s/
Truth table0%0/3
A or B
A= /^\s+\d+ tty \d+\s/B= /^\s+Se\d+\:\d+\s/
ABresult
1X1
011
000
132 0 $port = unpack("x0 a12", $_) ; $port =~ s/^\s*\d* //; $port =~ s/\s*$//;
133 0 $user = unpack("x13 a10", $_); $user =~ s/^\s*//; $user =~ s/\s*$//;
134 0 ▶ ($user eq $username) && push(@ports,$port);
Branch: 1 branch
BranchTrueFalseTotal
$user eq $username and push @ports, $port 0 0 0
135 }
136 0 return(@ports);
137 }
138  
139  
140 # userports() returns a hash of arrays
141 # keys are the usernames of all users currently connected
142 # values are arrays of ports that that user in connected on
143 0 ▶sub userports {
Subroutine userports: not called CC=4 SCAR=30.0
Pod documented
144 0 my($ras) = shift;
145 0 my($output) = $ras->run_command('show users');
146 0 my(%userports);
147  
148 0 foreach (@$output) {
149 0 my($port,$user);
150 0 ▶ next unless (/^\s+\d+ tty \d+\s/ || /^\s+Se\d+\:\d+\s/);
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s+\d+ tty \d+\s/ or /^\s+Se\d+\:\d+\s/ 0 0 0
Condition0%0/3
/^\s+\d+ tty \d+\s/ or /^\s+Se\d+\:\d+\s/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\s+\d+ tty \d+\s/ or /^\s+Se\d+\:\d+\s/
/^\s+\d+ tty \d+\s/ /^\s+Se\d+\:\d+\s/
Truth table0%0/3
A or B
A= /^\s+\d+ tty \d+\s/B= /^\s+Se\d+\:\d+\s/
ABresult
1X1
011
000
151 0 $port = unpack("x0 a12", $_) ; $port =~ s/^\s*\d* //; $port =~ s/\s*$//;
152 0 $user = unpack("x13 a10", $_); $user =~ s/^\s*//; $user =~ s/\s*$//;
153 0 push(@{$userports{$user}},$port);
154 }
155 0 return(%userports);
156 }
157  
158  
159 # portusage() returns an array: 1st element is the # of ports and the
160 # rest of the elements are usernames of users currently logged on
161 0 ▶sub portusage {
Subroutine portusage: not called CC=5 SCAR=34.0
Pod documented
162 0 my($ras) = shift;
163 0 my($interfaces,$connections) = $ras->run_command('sho isdn status','show users');
164 0 my(@users, $totalports);
165  
166 0 $totalports = 23 * scalar(grep(/^ISDN Serial\S+ interface$/, @$interfaces));
167  
168 0 foreach (@$connections) {
169 0 my($port,$user);
170 0 ▶ next unless (/^\s+\d+ tty \d+ / || /^\s+Se\d+\:\d+ /);
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s+\d+ tty \d+ / or /^\s+Se\d+\:\d+ / 0 0 0
Condition0%0/3
/^\s+\d+ tty \d+ / or /^\s+Se\d+\:\d+ /
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\s+\d+ tty \d+ / or /^\s+Se\d+\:\d+ /
/^\s+\d+ tty \d+ / /^\s+Se\d+\:\d+ /
Truth table0%0/3
A or B
A= /^\s+\d+ tty \d+ /B= /^\s+Se\d+\:\d+ /
ABresult
1X1
011
000
171 0 $user = unpack("x13 a10", $_); $user =~ s/^\s*(\S+)\s*$/$1/;
172 0 ▶ next if ($user =~ /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $user =~ /^\s*$/ 0 0 0
173 0 push(@users,$user);
174 }
175  
176 0 return($totalports,@users);
177 }
178  
179  
180 # userkill() does a usergrep() and then disconnects the specified user
181 0 ▶sub userkill {
Subroutine userkill: not called CC=7 SCAR=40.3
Pod documented
182 0 my($ras) = shift;
183 0 ▶ my($username); $username = shift; return() unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
184 0 ▶ if ($ras->{truncateusernames}) { $username = substr($username,0,10); }
Branch: 1 branch
BranchTrueFalseTotal
if ($ras->{"truncateusernames"}) 0 0 0
185 0 my(@ports) = $ras->usergrep($username);
186 0 ▶ return('') unless @ports;
Branch: 1 branch
BranchTrueFalseTotal
unless @ports 0 0 0
187  
188 0 my(@killcommands);
189 0 foreach (@ports) {
190 0 ▶ if (/^tty/) { push(@killcommands, "ENABLE clear line $_"); }
Branch: 2 branches
BranchTrueFalseTotal
if (/^tty/) { } 0 0 0
elsif (/^Se/) { } 0 0 0
191 0 elsif (/^Se/) { push(@killcommands, "ENABLE clear int $_"); }
192 }
193  
194 0 $ras->run_command(@killcommands);
195 0 return(@ports);
196 }
197  
198  
199 # killexcessoutoctets() takes a bytelimit and if a interfaces
200 # outoctets/bytes exceeds that limit the interface is cleared and then the
201 # counters for the interface are reset. Used to stop radius counters
202 # wrapping ie. 32 bit signed int wraps into negative at about 2 gig
203 0 ▶sub killexcessoutoctets {
Subroutine killexcessoutoctets: not called CC=6 SCAR=37.4
Pod documented
204 0 my($ras) = shift;
205 0 my($bytelimit) = shift;
206 0 my %userports = $ras->userports;
207  
208 0 foreach $user (keys %userports) {
209 0 foreach $arraycnt (0 .. (@{$userports{$user}} - 1)) {
210 0 my($result) = $ras->run_command('show interface ' . $userports{$user}[$arraycnt]);
211 0 foreach (@$result) {
212 0 ▶ next unless (/\s+\d+ packets output, (\d+) bytes.+/);
Branch: 1 branch
BranchTrueFalseTotal
unless /\s+\d+ packets output, (\d+) bytes.+/ 0 0 0
213 0 my $outoctets = $1;
214 0 ▶ if ($outoctets > $bytelimit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outoctets > $bytelimit) 0 0 0
215 0 $ras->run_command('ENABLE clear int ' . $userports{$user}[$arraycnt] , 'ENABLE clear counter ' . $userports{$user}[$arraycnt]);
216 0 next;
217 }
218 }
219 }
220 }
221 0 return(1);
222 }
223  
224 #############################################################
225  
226 1 1;#So PERL knows we're cool
227  
228 __END__;
↑ Summary