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

Hi all. I neew you wisdom. I'm going to pool status information from network printers. I'm using code:
foreach my $k (@ips) { my $hostname = "10.0.0.$k"; my $password = "public"; my ( $session, $error ) = Net::SNMP->session ( Hostname => $hostname, +Community => $password , Version => 1 ); if (!defined($session)) { printf("ERROR: %s.\n", $error); next; } }
in @ips I have last octet of IP's The problem is if one of the printer is swiched off the programm break Could you tell me what I'm doing wrong? Thanks in advance!

Replies are listed 'Best First'.
Re: Problems with the module Net::SNMP
by andyford (Curate) on Nov 07, 2006 at 10:37 UTC
    If I remember correctly, creating a session doesn't actually start communication with the target device, so the error value of creating the session is not telling you if the device is turned on or not.
    You have to actually do a get() or something and wait for a timeout to deal with down devices.

    andyford
    or non-Perl: Andy Ford

      Set an example
Re: Problems with the module Net::SNMP
by Anonymous Monk on Nov 07, 2006 at 09:56 UTC
    If that's your entire program, I don't see the point of it. I also don't see where it would 'break'.

    If it's not the entire program, perhaps you should post where it would 'break'. And explain what you mean by 'break'.

      my @ips=qw(11 12 28);
      If one their printers that does not answer stops with a mistake:( It would be desirable completely all array.
Re: Problems with the module Net::SNMP
by monarch (Priest) on Nov 07, 2006 at 15:39 UTC
    I notice this is almost a repeat of this question that you asked one month ago.

    I thought my answer would have resolved your issues.. in what way can I expand this answer to better solve your query?

      this full code
      #!/usr/bin/perl use strict; use Net::SNMP; my @ips=qw(12 28 11); foreach my $k (@ips) { my $hostname = "10.0.0.$k"; my $password = "public"; my %param = ( sysUpTime => '.1.3.6.1.2.1.1.3', sysName => '.1.3.6.1.2.1.1.5', ifDescr => '.1.3.6.1.2.1.2.2.1.2', ifSpeed => '.1.3.6.1.2.1.2.2.1.5', ifPhysAddress => '.1.3.6.1.2.1.2.2.1.6', hrPrinterStatus => '.1.3.6.1.2.1.25.3.5.1.1', hrPrinterDetectedErrorState => '.1.3.6.1.2.1.25.3.5.1.2', hrDeviceDescr => '.1.3.6.1.2.1.25.3.2.1.3', prtCoverStatus => '.1.3.6.1.2.1.43.6.1.1.3', prtMarkerLifeCount => '.1.3.6.1.2.1.43.10.2.1.4' ); my $status; my ( my $session, my $error ) = Net::SNMP->session ( Hostname => $host +name, Community => $password , Version => 1 ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $result = $session->get_next_request ( varbindlist => [ $param{sysN +ame}.$1] ); die "request error: ".$session->error unless ( defined $r +esult ); ( $param{sysName}, my $name1 ) = each %$result; unless ( $param{sysNam +e} =~ /$param{sysName}(.*)/ ) { last; } $session->close; print "$k $name1\n"; } exit 0;
      host 10.0.0.28 power off
      I made a mistake. Programm give an error
      request error: No response from remote host '10.0.0.28' at ps-test.pl line 21.
        Line 21 is this line:

        my $result = $session->get_next_request ( varbindlist => [ $param{sysN +ame}.$1] ); die "request error: ".$session->error unless ( defined $r +esult );

        The reason that your program breaks is that it directly calls "die" which brings the script to an immediate end... see die for the details. I think that if you change it to "warn" or even a plain "print" it will do what you are looking for. For similar reasons, you will also need to change

        unless ( $param{sysName} =~ /$param{sysName}(.*)/ ) {last}

        on the next line to

        unless ( $param{sysName} =~ /$param{sysName}(.*)/ ) {next}

        since the "last" will exit the loop so no more ip's will be tried.