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

Inspired by fingers' recent post, I decided it was time to tinker with Net::SNMP.   Snippet below shows my initial efforts.

The commented-out version at the bottom works fine, but is chock-full of evil copy+paste.   The section at the top is an attempt to use a tied hash to eliminate redundant code, but results in

Use of uninitialized value at netsnmpTinker.pl line 34 sysDescr for host 'localhost' is Use of unitialized value at netsnmpTinker.pl line 34. sysUpTime for host 'localhost' is Use of unitialized value at netsnmpTinker.pl line 34. ifNumber for host 'localhost' is Use of unitialized value at netsnmpTinker.pl line 34. ifTable for host 'localhost' is
Near as I can tell, is choking on $snmp{response}->{$mib{key}},   What might I be doing wrong here?
    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")

#! /usr/bin/perl -w # netsnmpTinker.pl # ybiC 2001-05-18 # derived from Net::SNMP example1.pl use strict; use Net::SNMP; use Tie::IxHash; my %snmp; ($snmp{session}, $snmp{error}) = Net::SNMP->session( -hostname => shift || 'localhost', -community => shift || 'public', -port => shift || 161 ); print "\n"; unless (defined($snmp{session})) { printf("ERROR: %s.\n", $snmp{error}); exit 1; } # =cut tie my %mib, "Tie::IxHash"; %mib = ( sysDescr => '1.3.6.1.2.1.1.1.0', sysUpTime => '1.3.6.1.2.1.1.3.0', ifNumber => '1.3.6.1.2.1.2.1.0', ifTable => '1.3.6.1.2.1.2.2.0', ); foreach my $key (keys %mib) { if (defined($snmp{response} = $snmp{session}->get_request($mib{key +}))) { printf("$key for host '%s' is %s\n", $snmp{session} ->hostname(), $snmp{response}->{$mib{key}}, ); } else { printf("ERROR: %s.\n", $snmp{session}->error()) } } $snmp{session}->close(); print "\n"; exit 0; =cut my $sysDescr = '1.3.6.1.2.1.1.1.0'; my $sysUpTime = '1.3.6.1.2.1.1.3.0'; my $ifNumber = '1.3.6.1.2.1.2.1.0'; my $ifTable = '1.3.6.1.2.1.2.2.0'; if (defined($snmp{response} = $snmp{session}->get_request($sysDescr))) + { printf("sysDescr for host '%s' is %s\n", $snmp{session}->hostname(), $snmp{response}->{$sysDescr} ); } else { printf("ERROR: %s.\n", $snmp{session}->error()) } if (defined($snmp{response} = $snmp{session}->get_request($sysUpTime)) +) { printf("sysUpTime for host '%s' is %s\n", $snmp{session}->hostname(), $snmp{response}->{$sysUpTime} ); } else { printf("ERROR: %s.\n", $snmp{session}->error()) } if (defined($snmp{response} = $snmp{session}->get_request($ifNumber))) + { printf("ifNumber for host '%s' is %s\n", $snmp{session}->hostname(), $snmp{response}->{$ifNumber} ); } else { printf("ERROR: %s.\n", $snmp{session}->error()); } if (defined($snmp{response} = $snmp{session}->get_request($ifTable))) +{ printf("ifTable for host '%s' is %s\n", $snmp{session}->hostname(), $snmp{response}->{$ifTable} ); } else { printf("ERROR: %s.\n", $snmp{session}->error()); } print "\n"; $snmp{session}->close(); exit 0; =cut

Replies are listed 'Best First'.
Re: uninitialized value with Tie::IxHash and Net::SNMP
by chipmunk (Parson) on May 18, 2001 at 18:36 UTC
    Near as I can tell, is choking on $snmp{response}->{$mib{key}}, What might I be doing wrong here?
    I'm guessing that should be $snmp{response}->{$mib{$key}}.
      Thanks and ++ to danger and chipmunk for pointing out my blind spot: second $ in $mib{$key}   Working code snippet below (tweaked a bit more).
          cheers,
          Don
          striving toward Perl Adept
          (it's pronounced "why-bick")
      #! /usr/bin/perl -w # netsnmpTinker.pl # ybiC 2001-05-18 13:30 # derived from Net::SNMP example1.pl # online MIB browser at http://www.ibr.cs.tu-bs.de/cgi-bin/sbrowser.cg +i use strict; use Net::SNMP; use Tie::IxHash; my %snmp; ($snmp{session}, $snmp{error}) = Net::SNMP->session( -hostname => shift || 'localhost', -community => shift || 'public', -port => shift || 161, ); tie my %mib, "Tie::IxHash"; %mib = ( # sysDescr => '1.3.6.1.2.1.1.1.0', sysUpTime => '1.3.6.1.2.1.1.3.0', sysContact => '1.3.6.1.2.1.1.4.0', sysName => '1.3.6.1.2.1.1.5.0', sysLocation => '1.3.6.1.2.1.1.6.0', ifNumber => '1.3.6.1.2.1.2.1.0', cdpAddress => '1.3.6.1.4.1.9.9.23.1.2.1.1.4', cdpDeviceID => '1.3.6.1.4.1.9.9.23.1.2.1.1.6', cdpHWtype => '1.3.6.1.4.1.9.9.23.1.2.1.1.8', ); unless (defined($snmp{session})) { printf(" Error: %s.\n", $snmp{error}); exit 1; } printf("\n Host = %s\n", $snmp{session} ->hostname()); for(keys %mib) { if (defined($snmp{response} = $snmp{session}->get_request($mib{$_}) +)) { printf(" $_ = %s\n", $snmp{response}->{$mib{$_}}); } else { print " $_ = not responding\n"} } $snmp{session}->close(); print "\n"; exit 0;