in reply to Re^2: Where is the bug in this Net::SNMP code?
in thread Where is the bug in this Net::SNMP code?
I presume you wish to poll printer information using SNMP.
I assume that
You want to poll two objects. From the RFC:
prtCoverEntry OBJECT-TYPE SYNTAX PrtCoverEntry MAX-ACCESS not-accessible STATUS current DESCRIPTION "Information about a cover or interlock. Entries may exist in the table for each device index whose device type is `printer'." INDEX { hrDeviceIndex, prtCoverIndex } ::= { prtCoverTable 1 } prtCoverDescription OBJECT-TYPE SYNTAX OCTET STRING (SIZE(0..255)) MAX-ACCESS read-only STATUS current DESCRIPTION "The manufacturer provided cover sub-mechanism name in the localization specified by prtGeneralCurrentLocalization." ::= { prtCoverEntry 2 } prtCoverStatus OBJECT-TYPE -- This value is a type 2 enumeration SYNTAX INTEGER { other(1), doorOpen(3), doorClosed(4), interlockOpen(5), interlockClosed(6) } MAX-ACCESS read-only STATUS current DESCRIPTION "The status of this cover sub-unit." ::= { prtCoverEntry 3 }
This is a table, indexed by two integers.
To read this table I would do the following:
use Net::SNMP; use strict; my %oid = ( prtCoverStatus => '1.3.6.1.2.1.43.6.1.1.3', prtCoverDescription => '1.3.6.1.2.1.43.6.1.1.2', ); my %prtCoverStatusEnum = ( 1 => 'other(1)', 3 => 'doorOpen(3)', 4 => 'doorClosed(4)', 5 => 'interlockOpen(5)', 6 => 'interlockClosed(6)', ); sub query_printer( $ ) { my ( $printer_ip ) = @_; my ( $session, $error ) = Net::SNMP->session( -hostname => $printer_ip, -community => 'public', -version => 1, ); die( "Could not create session: $error" ) if ( ! $session ); my $index = "0"; while ( 1 ) { my @oidlist = ( $oid{prtCoverStatus} . ".$index" ); my $result = $session->get_next_request( -varbindlist => \@oidlist, ); if ( ! $result ) { last; # maybe there are no more objects } # we expect just one key to be returned my $prtCoverStatus = undef; foreach ( keys %{$result} ) { if ( $_ =~ m/^\Q$oid{prtCoverStatus}\E\.([^.])\.([^.])+$/ ) { my $hrDeviceIndex = $1; my $prtCoverIndex = $2; $index = "$hrDeviceIndex.$prtCoverIndex"; $prtCoverStatus = $result->{$_}; last; } } if ( ! $prtCoverStatus ) { last; # we've probably come to the end of the table } # now get other objects from the SAME TABLE @oidlist = ( $oid{prtCoverDescription} . ".$index" ); $result = $session->get_request( -varbindlist => \@oidlist, ); if ( ! $result ) { die( "Error getting object with index $index: " . $session->error() ); } my $prtCoverDescription = $result->{$oidlist[0]}; if ( exists( $prtCoverStatusEnum{$prtCoverStatus} ) ) { $prtCoverStatus = $prtCoverStatusEnum{$prtCoverStatus}; } print( "Cover description: $prtCoverDescription\n" ); print( " status: $prtCoverStatus\n" ); print( "\n" ); } # while there are rows in the SNMP cover table $session->close(); } # query_printer foreach ( "10.0.0.11", "10.0.0.12" ) { print( "QUERYING PRINTER $_\n" ); query_printer( $_ ); }
The output I get when querying the printer on my local LAN is:
C:\temp>perl -w checkprintercover.pl Cover description: Copier cover status: doorClosed(4) Cover description: Finisher cover status: doorClosed(4)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Where is the bug in this Net::SNMP code?
by tolyan77 (Novice) on Oct 10, 2006 at 12:09 UTC | |
|
Re^4: Where is the bug in this Net::SNMP code?
by tolyan77 (Novice) on Nov 04, 2006 at 12:19 UTC |