#!/usr/bin/perl ###################################################################### ### Sample CGI script using the Perl 5 SNMP Module ### ### This script can be used as a CGI script with an HTTP Daemon, to ### allow asking SNMP queries from a World-Wide Web client. ### ###################################################################### ### When called with an empty QUERY_STRING environment variable, a ### form is generated that lets the user fill in a host and community ### name. When the filled-in form is submitted, the script will be ### called again, this time with parameters passed through ### $QUERY_STRING. It will make an SNMP query to the selected ### host/community and return the results as an HTML document ### containing an HTML 3 table which shows the names and values of ### some MIB variable instances. ###################################################################### require 5; require 'SNMP_Session.pm'; use BER; sub parse_query; sub init_oids; sub query_to_html_response; sub snmp_get; sub write_query_form; sub html_error_message; sub html_quote; my @allowed_hosts=qw(swiEG1.switch.ch swiCS1.switch.ch swiZHX.switch.ch swiCE1.switch.ch swiNY1.switch.ch); my %allowed_hosts; my $intro = < This is a sample application of an SNMP Module I have written for Perl 5.

EOM foreach (@allowed_hosts) { $allowed_hosts{$_} = 1; } my $query_string = $ENV{'QUERY_STRING'}; if (!defined ($query_string) || !$query_string) { write_query_form (); } else { my (%query) = parse_query (); init_oids (); if (! defined ($allowed_hosts{$query{'hostname'}})) { print "Content-type: text/html\n\n"; html_error_message ("parsing the query", "Illegal hostname"); } else { query_to_html_response ($query{'hostname'}, $query{'community'}); } } 1; sub parse_query { my (%query) = (()); foreach (split('&',$query_string)) { ($lhs,$rhs) = split('=',$_,2); $query{$lhs} = $rhs if $rhs; } return %query; } sub init_oids { %ugly_oids = qw(sysDescr.0 1.3.6.1.2.1.1.1.0 sysLocation.0 1.3.6.1.2.1.1.6.0 ); foreach (keys %ugly_oids) { $ugly_oids{$_} = encode_oid (split (/\./, $ugly_oids{$_})); $pretty_oids{$ugly_oids{$_}} = $_; } } sub query_to_html_response { local($hostname, $community) = @_; if ($community eq 'public' && -r "/home/leinen/snmp/.zxc") { open (COMM, "/home/leinen/snmp/.zxc"); $community = ; chomp $community; close COMM; } print "Content-type: text/html\n\n", "Perl SNMP Module Test\n", "\n\n", "

SNMP query to ", html_quote ($community), "@", html_quote ($hostname), "

\n
\n"; srand(); eval '$session = SNMP_Session->open ($hostname, $community, 161)'; html_error_message ("opening SNMP session", $@), return 0 if $@; eval { snmp_get ($session, qw(sysDescr.0 sysLocation.0)) }; html_error_message ("executing SNMP query", $@), return 0 if $@; $session->close (); print "\n\n"; 1; } sub snmp_get { my($session, @oids) = @_; my($response, $bindings, $binding, $value, $oid); grep ($_ = $ugly_oids{$_}, @oids); if ($session->get_request_response (@oids)) { $response = $session->pdu_buffer; ($bindings) = $session->decode_get_response ($response); print "\n"; while ($bindings ne '') { ($binding,$bindings) = decode_sequence ($bindings); ($oid,$value) = decode_by_template ($binding, "%O%@"); print "", "", "", "\n"; } print "
", $pretty_oids{$oid}, "
", &html_quote (pretty_print ($value)), "
\n"; 1; } else { die "No response received.\n"; } } sub write_query_form { print < Perl SNMP Module Test

Perl SNMP Module Test


$intro
Host name: Community name:

Simon Leinen <simon\@switch.ch>
EOM } sub html_error_message { my($context, $errmsg) = @_; print "

SNMP Error

\n
\n"; print "

While ",$context,", the following error occurred:

\n"; print "
",&html_quote($errmsg),"
"; } sub html_quote { local ($_) = @_; return $_ unless /[<>&]/; s/&/&/g; s//>/g; $_; }