File: if-status.pl

package info (click to toggle)
libsnmp-session-perl 1.14~git20221124T101957-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,104 kB
  • sloc: perl: 11,920; ansic: 25; makefile: 15
file content (137 lines) | stat: -rwxr-xr-x 3,423 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/local/bin/perl -w
#
# Small sample application for table walking
#
# Given a router name and SNMP community string, traverse the RFC1213
# ifTable and write a message for each interface whose ifOperStatus
# differs from the ifAdminStatus.

require 5.003;

use strict;

use lib qw(/opt/www/cgi-bin/cug/switch/snmp);

use BER;
use SNMP_Session;
use CGI qw(:html2 :html3);

### Forward declarations
sub out_interface ($$$$$ );
sub usage ();
sub print_html_header ($ );
sub print_html_trailer ();

my $output_mode = 'text';
my $have_html_header = 0;

my ($host, $community);
my $query;
my $version;

if (defined $ENV{'REQUEST_METHOD'}) {
  $output_mode = 'html';
  $query = new CGI;

  $host = $query->param ('host');
  $community = $query->param ('community');
  print $query->header;
} else {
  my $arg;

  while (defined $ARGV[0] and $ARGV[0] =~ /^-/) {
    if ($ARGV[0] =~ /^-v/) {
	if ($ARGV[0] eq '-v') {
	    shift @ARGV;
	    usage () unless defined $ARGV[0];
	} else {
	    $ARGV[0] = substr($ARGV[0], 2);
	}
	if ($ARGV[0] eq '1') {
	    $version = '1';
	} elsif ($ARGV[0] eq '2c' or $ARGV[0] eq '2') {
	    $version = '2c';
	} else {
	    usage ();
	}
    } elsif ($arg eq '-html') {
      $output_mode = 'html';
    } else {
      usage ();
    }
    shift @ARGV;
  }
  $host = shift @ARGV || die;
  $community = shift @ARGV || die;
}

my $ifDescr = [1,3,6,1,2,1,2,2,1,2];
my $ifAdminStatus = [1,3,6,1,2,1,2,2,1,7];
my $ifOperStatus = [1,3,6,1,2,1,2,2,1,8];
my $locIfDescr = [1,3,6,1,4,1,9,2,2,1,1,28];

my $the_router;

sub out_interface ($$$$$) {
    my ($index, $descr, $admin, $oper, $comment) = @_;
    grep (defined $_ && ($_=pretty_print $_),
	  ($descr, $admin, $oper, $comment));

    return if defined $admin && $admin == 2;
    return if defined $admin && defined $oper && $admin == $oper;

    my $admin_string = $admin ? ($admin == 1 ? 'up' : ($admin == 2 ? 'down' : "?($admin)")) : "?";
    my $oper_string = $oper ? ($oper == 1 ? 'up' : ($oper == 2 ? 'down' : "?($oper)")) : "?";
    $comment = '' unless defined $comment;

    if ($output_mode eq 'text') {
	printf "%2d  %-20s %10s %10s %s\n",
	$index, $descr, $admin_string, $oper_string,
	defined $comment ? $comment : '';
    } elsif ($output_mode eq 'html') {
	print_html_header ($the_router), $have_html_header = 1
	    unless $have_html_header;
	print TR(th($index),
		 td(CGI->escapeHTML($descr)),
		 td({align=>'center'},$admin_string),
		 td({align=>'center'},$oper_string),
		 td(CGI->escapeHTML($comment))),"\n";
    }
}

$the_router = $host;

my $session =
    ($version eq '1' ? SNMPv1_Session->open ($host, $community, 161)
     : $version eq '2c' ? SNMPv2c_Session->open ($host, $community, 161)
     : die "Unknown SNMP version $version")
  || die "Opening SNMP_Session";
$session->map_table ([$ifDescr,$ifAdminStatus,$ifOperStatus,$locIfDescr],
		     \&out_interface);

if ($output_mode eq 'html' && $have_html_header) {
  print_html_trailer ();
}
1;

sub usage () {
  die "Usage: $0 [-html] hostname community";
}

sub print_html_header ($ ) {
  my ($router) = @_;
  print "<html>\n";
  print head(title("Interface Listing for ".$router)),"\n";
  print " <BODY BGCOLOR=\"#ffffff\">\n",
	     h1("Interface Listing for ".tt($router)),"\n",
	     "  <TABLE>\n",
	     TR(th("index"),
		th("descr"),
		th("admin"),
		th("oper"),
		th("description"));
}

sub print_html_trailer () {
  print "  </table>\n </body>\n</html>\n";
}