SNMP module vs Net::SNMP module

If you're fortunate to get stuck working on IPv6 stuff you're going to find that things are not always as they seem.

In my job where I'm tasked with testing scripts using SNMP to verify that they work with IPv6 addresses as well as IPv4 addresses I've found that the SNMP and Net::SNMP perl docs aren't necessarily correct on some things. Fortunately you can use Net::SNMP to create a session where it returns an error message if it doesn't create a session unlike SNMP which doesn't. SNMP appears to be more of a quick wrapper

This will help you overcome any shortcomings you have with networking and get you on your merry way.

Here's an excerpt from the Net::SNMP perl doc from CPAN:
     ($session, $error) = Net::SNMP->session(
                                                           -hostname => $hostname, # works
                                                           Hostname => $hostname, # doesn't
                                                            . . .

Now what's kind of funny, at first glance, is that all the parameters are optional which honestly doesn't sound right unless you intentionally want it to connect to your localhost which you might actually want to do. Now I recommend using Data::Dumper and then do:
print "Session:",Dumper($session),"\nError:",Dumper($error);
and the $error will be clear enough to actually figure out what is wrong and correct it. Once you have it worked out properly then you can convert it back to an SNMP style:
       my $session = SNMP::Session->new(
                                                               -hostname => $ip, # works
                                                                Hostname => $ip, # doesn't
and you've got it.

And if you might have Version 3 SNMP requests then you might try:
                                                               -version => $version,
                                                               -authprotocol => $version =~ /3/
                                                                                               ? $authproto : '',
                                                               -authpassword => $version =~ /3/
                                                                                               ? $authpasswd : '',

This is also very close to the way Perl::Tidy formats it too.

Now if you wrap this within an conditional so you can distinguish between v4 & v6
if ($hostname =~ /\w/) {
      # new style
else {
      # your existing IPv4 style which doesn't seem to be as picky

I'm sure I've missed some key point in the docs but networking always twists my head into a knot so it could be as simple as some people like to put single quotes around keys in a hash while others realise you aren't required unless it contains spaces so it's really more of a style thing.

Now it would be nice to have more examples in CPAN docs but honestly having any docs is far preferable to having none at all. I also have the same complaint with man pages as they too don't often have good examples if they have examples at all.

1 Comment

Thanks for this interesting post. Have you looked at using SNMP::Info ?

I am one of the maintainers so please do provide any feedback, good or bad, if you do try it out.

Leave a comment

About Craig Steffler

user-pic Perl/Ruby on Rails/PHP, every DB under the sun, Unix/Linux/Windows (but SuSE beats most)