in reply to Re: Problems with the module Net::SNMP
in thread Problems with the module Net::SNMP

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.

Replies are listed 'Best First'.
Re^3: Problems with the module Net::SNMP
by quester (Vicar) on Nov 08, 2006 at 09:26 UTC
    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.

      if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; }
      I thought the programm must stop on this line?
        Oops, sorry... yes, you need to change "exit 1" on this line to "next" so the program will continue in this case too.