File Coverage

blib/lib/RAS/PortMaster.pm
Criterion Covered Total %
statement 6 80 7.5
branch 0 24 0.0
condition n/a
subroutine 2 8 25.0
pod 6 6 100.0
total 14 118 11.8


line stmt bran cond sub pod time code
1             ### RAS::PortMaster.pm
2             ### PERL 5 module for accessing a Livingston PortMaster
3             #########################################################
4              
5             package RAS::PortMaster;
6             $RAS::PortMaster::VERSION = "1.16";
7              
8 1     1   670 use strict;
  1         2  
  1         38  
9              
10             # This uses Net::Telnet to connect to the RAS
11 1     1   1761 use Net::Telnet ;
  1         50517  
  1         6038  
12              
13             # The name $ras will be used consistently as the
14             # reference to the RAS::PortMaster object we're handling
15              
16              
17             # The constructor method, of course
18             sub new {
19 0     0 1   my($class) = shift ;
20 0           my($ras) = {} ;
21 0           %{$ras} = @_ ;
  0            
22 0           $ras->{'VERSION'} = $RAS::PortMaster::VERSION;
23 0           bless($ras);
24             }
25              
26              
27             # for debugging - printenv() prints to STDERR
28             # the entire contents of %$ras
29             sub printenv {
30 0     0 1   my($ras) = shift;
31 0           while (my($key,$value) = each(%{$ras})) { print "$key = $value\n"; }
  0            
  0            
32             }
33              
34              
35             # This runs the specified commands on the router and returns
36             # a list of refs to arrays containing the commands' output
37             sub run_command {
38 0     0 1   my($ras) = shift;
39 0           my(@returnlist);
40              
41 0           while (my $command = shift) {
42 0           my($session) = new Net::Telnet;
43 0           $session->errmode("return");
44              
45             # connect
46 0           $session->open($ras->{'hostname'});
47 0 0         if ($session->errmsg) {
48 0           warn "ERROR: ",ref($ras),' - ',$session->errmsg,"\n"; return(); }
  0            
49             # login
50 0           $session->login($ras->{'login'},$ras->{'password'});
51 0 0         if ($session->errmsg) {
52 0           warn "ERROR: ",ref($ras),' - ',$session->errmsg,"\n"; return(); }
  0            
53             # we got logged in, so send the command
54 0           $session->print($command);
55 0           $session->print(""); # for some reason, this is necessary to make the prompt appear after the output
56              
57 0           my(@output);
58 0           while (1) {
59 0           my($line) = $session->getline;
60 0 0         if ($session->errmsg) {
61 0           warn "ERROR: ",ref($ras),' - ',$session->errmsg,"\n"; return(); }
  0            
62 0 0         if ($line =~ /^\s*$/) { next; }
  0            
63 0 0         if ($line =~ /^-- Press Return for More --\s+$/) { $session->print(""); next; }
  0            
  0            
64 0 0         if ($line =~ /^[\w\.\-]+\>\s+$/) { $session->print("quit"); $session->close; last; }
  0            
  0            
  0            
65 0           push(@output, $line);
66             }
67              
68              
69             # Net::Telnet to the PM leaves the echoed command at the start
70 0           shift(@output);
71 0           push(@returnlist, \@output);
72             } # end of shifting commands
73              
74             # We're returning a list of references to lists.
75             # Each ref points to an array containing the returned text
76             # from the command, and the list of refs corresponds
77             # to the list of commands we were given
78 0           return(@returnlist);
79             } # end of run_command
80              
81              
82             # usergrep() - takes a username and returns an array of
83             # ports on which the user was found
84             sub usergrep {
85 0     0 1   my($ras) = shift;
86 0 0         my($username) = shift; return() unless $username;
  0            
87 0           my($output) = $ras->run_command('sho ses');
88 0           my(@ports);
89              
90 0           foreach (@$output) {
91 0 0         next unless /^(S\d+)\s+$username\s+/;
92 0           push(@ports, $1);
93             }
94 0           return(@ports);
95             }
96              
97              
98             # portusage() returns a list: # of ports, list of users
99             sub portusage {
100 0     0 1   my($ras) = shift;
101 0           my($output) = $ras->run_command('sho ses');
102 0           my(@users,$totalports);
103 0           $totalports = 0;
104              
105 0           foreach (@$output) {
106 0 0         next unless /^S\d+\s+(\S+)\s+/;
107 0           $totalports++;
108 0 0         next if ($1 =~ /^PPP|\-$/);
109 0           push(@users, $1);
110             }
111 0           return($totalports,@users);
112             }
113              
114              
115             # This does a usergrep() and then disconnects the specified user
116             sub userkill {
117 0     0 1   my($ras) = shift;
118 0 0         my($username); $username = shift; return() unless $username;
  0            
  0            
119 0           my(@ports) = $ras->usergrep($username);
120 0 0         return('') unless @ports;
121              
122 0           my @resetcmd = ();
123 0           foreach (@ports) { push(@resetcmd,"reset $_"); }
  0            
124 0           $ras->run_command(@resetcmd);
125              
126 0           return(@ports);
127             }
128              
129              
130             #############################################################
131             1;#So PERL knows we're cool
132             __END__;