tolyan77 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. There is a code and in it at me the mistake jumps out prompt where a mistake.
#!/usr/bin/perl use Net::SNMP; my @ips=(11,21); foreach my $k (@ips) { my $hostname = "10.0.0.$k"; my $password = "public"; my $sysUpTime = "1.3.6.1.2.1.1.3"; my $sysName = "1.3.6.1.2.1.1.5"; my $prtCoverStatus = "1.3.6.1.2.1.43.6.1.1.3"; my $ObjectID1, $ObjectID2 ; my $status; my ( $session, $error ) = Net::SNMP->session ( Hostname => $hostname, + Community => $password ); die "session error: $error" unless ( $session ); $ObjectID1 = $sysName; $result = $session->get_next_request ( varbindlist => [$ObjectID1] ); die "request error: ".$session->error unless ( defined $result ); $ObjectID1 = $sysName; ( $ObjectID1, $name ) = each %$result; unless ( $ObjectID1 =~ /$sysName(.*)/ ) { last; } $ObjectID2 = $prtCoverStatus.$1; $result = $session->get_request ( varbindlist => [$ObjectID2] ); die "request error: ".$session->error unless ( defined $result ); #request error: Received noSuchName(2) error-status at error-index 1 a +t status.pl ... $CoverStatus = "$result->{$ObjectID2}"; $session->close; }
Please help me out. thanks in advance.

2006-10-09 Retitled by Arunbear, as per Monastery guidelines
Original title: 'net::snmp'

Replies are listed 'Best First'.
Re: Where is the bug in this Net::SNMP code?
by monarch (Priest) on Oct 09, 2006 at 13:46 UTC
    I had a look at your code. There could be many problems as you have not described what errors or symptoms you're receiving.

    First, the good news. You've got lots of error checking there and die statements to protect you from bad coding. Well done, I can only encourage this!

    The bad news: you do a getnext on sysName. Thus I presume you will get sysName.0 because all scalar SNMP objects (ie objects that are NOT columnar), by definition, have a trailing .0 in the object identifier.

    However, assuming sysName is a columnar object, you still have problems because you're assuming that the sysName column is in the same table as the prtCoverStatus. And they clearly are NOT in the same table because their OIDs (object identifiers) are different lengths let alone in different parts of the address space.

    I could be wrong, however. Maybe the sysName object is in a table with an index of type OCTET STRING. So you're trying to capture the name of a device from the table the sysName object is in and then look in the table the prtCoverStatus object is in that is presumably indexed by device name as an OCTET STRING too???

    Can you see that you need to assist us by describing a) what is your objective and b) how, specifically, is this failing?


    Lastly - the last line of your script closes your SNMP object after it first discovers an answer. Is this what you mean to do? Or do you mean to close the SNMP object AFTER polling ALL devices?

      "Lastly - the last line of your script closes your SNMP object after it first discovers an answer. Is this what you mean to do? Or do you mean to close the SNMP object AFTER polling ALL devices?"
      monarch i don't understand your question:(
      monarch
      Prompt how to take data from .1.3.6.1.2.1.25.3.5.1.2 if it OCTET STRING
      hrPrinterDetectedErrorState OBJECT-TYPE SYNTAX OCTET STRING MAX-ACCESS read-only STATUS current DESCRIPTION "This object represents any error conditions detected by the printer. The error conditions are encoded as bits in an octet string, with the following definitions: Condition Bit # lowPaper 0 noPaper 1 lowToner 2 noToner 3 doorOpen 4 jammed 5 offline 6 serviceRequested 7 inputTrayMissing 8 outputTrayMissing 9 markerSupplyMissing 10 outputNearFull 11 outputFull 12 inputTrayEmpty 13 overduePreventMaint 14 Bits are numbered starting with the most significant bit of the first byte being bit 0, the least significant bit of the first byte being bit 7, the most significant bit of the second byte being bit 8, and so on. A one bit encodes that the condition was detected, while a zero bit encodes that the condition was not detected. This object is useful for alerting an operator to specific warning or error conditions that may occur, especially those requiring human intervention." -- 1.3.6.1.2.1.25.3.5.1.2 ::= { hrPrinterEntry 2 }
      It is possible an example of a code?
      Please help me out. thanks in advance.
        It appears you have little understanding of SNMP in general. The hrPrinterDetectedErrorState object is defined in RFC1759 (the RFC is more readable at this link).

        I presume you wish to poll printer information using SNMP.

        I assume that

        • the IP address you're polling is that of a printer
        • you don't need to know the name of the printer
        • you just want the information from the printer

        You want to poll two objects. From the RFC:

        This is a table, indexed by two integers.

        To read this table I would do the following:

        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)
Re: Where is the bug in this Net::SNMP code?
by shmem (Chancellor) on Oct 09, 2006 at 12:06 UTC
    To debug, I'd start looking at the capture in this line
    unless ( $ObjectID1 =~ /$sysName(.*)/ ) { last; } # does this what you want? -----^^^^
    and check whether what's in $1 is fit to be appended, in the following line:
    $ObjectID2 = $prtCoverStatus.$1;

    HTH,
    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I have made on another
      #!/usr/bin/perl #use strict; use Net::SNMP; my @ips=(11); foreach my $k (@ips) { my $hostname = "10.0.0.$k"; my $password = "public"; my $sysUpTime = '.1.3.6.1.2.1.1.3'; my $sysName = '.1.3.6.1.2.1.1.5'; my $hrPrinterStatus = '.1.3.6.1.2.1.25.3.5.1.1'; my $hrPrinterDetectedErrorState = '.1.3.6.1.2.1.25.3.5.1.2'; my $prtCoverStatus = '.1.3.6.1.2.1.43.6.1.1.3'; my $status; my ( $session, $error ) = Net::SNMP->session ( Hostname => $hostname, Community => $password ); die "session error: $error" unless ( $session ); $result = $session->get_next_request ( varbindlist => [$sysName.$1] ); + die "request error: ".$session->error unless ( defined $result ); ( $sysName, $name1 ) = each %$result; unless ( $sysName =~ /$sysName(. +*)/ ) { last; } $result = $session->get_next_request ( varbindlist => [$prtCoverStatus +.$1] ); die "request error: ".$session->error unless ( defined $resul +t ); ( $prtCoverStatus, $name2 ) = each %$result; unless ( $prtCoverStatus +=~ /$prtCoverStatus(.*)/ ) { last; } $result = $session->get_next_request ( varbindlist => [$hrPrinterDetec +tedErrorState.$1] ); die "request error: ".$session->error unless ( d +efined $result ); ( $hrPrinterDetectedErrorState, $name3 ) = each %$result; unless ( $hr +PrinterDetectedErrorState =~ /$hrPrinterDetectedErrorState(.*)/ ) { l +ast; } $result = $session->get_next_request ( varbindlist => [$hrPrinterStatu +s.$1] ); die "request error: ".$session->error unless ( defined $resu +lt ); ( $hrPrinterStatus, $name4 ) = each %$result; unless ( $hrPrinterStatu +s =~ /$hrPrinterStatus(.*)/ ) { last; } $session->close; print "$name1\t$name2\t$name3\t$name4\n"; } exit 0;
      There was other question why there are no data in .1.3.6.1.2.1.25.3.5.1.2, $name3 - it is empty.