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

In reply to uninitialized value with Tie::IxHash and Net::SNMP by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.