Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Net::SNMP result oddity

by mackdav (Novice)
on Jul 27, 2007 at 14:54 UTC ( [id://629120]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings o guides to wisdom

I am starting out with Net::SNMP. I am trying to query a switch's switching table. This goes OK, except it appears that Net::SNMP is garbling a couple of the MAC addresses when processing the result of the queries.

This is the fragment of code in question:

#!/usr/bin/perl use strict; use Net::SNMP qw(snmp_dispatcher oid_lex_sort); my $host = '3c39t1-01'; my $raw; my $key; my $value; my %macTable; my $TpFdbAddress = '1.3.6.1.2.1.17.4.3.1.1'; my $result; my ($session, $error) = Net::SNMP->session( -hostname => $ARGV[0] || $host, -community => $ARGV[1] || 'public', -port => $ARGV[2] || 161 ); if (!defined($session)) { printf("ERROR: %s\n", $error); exit 1; } if (defined($result = $session->get_table(-baseoid => $TpFdbAddress))) { foreach (oid_lex_sort(keys(%{$result}))) { $raw = $_; $value = $result->{$_}; $raw =~ s/$TpFdbAddress.//; $macTable{$raw} = $value; } } else { printf("ERROR: %s\n\n", $session->error()); } $session->close; foreach $key (sort(keys(%macTable))) { print "$key -> $macTable{$key}\n"; } exit 0;
Running it results in output like this:
[snip] 0.13.162.0.144.140 -> 0x000da200908c 0.13.86.118.96.80 -> ^@^MVv`P 0.13.86.171.103.33 -> 0x000d56ab6721 0.13.86.178.142.133 -> 0x000d56b28e85 0.13.86.225.88.22 -> 0x000d56e15816 0.13.86.32.12.60 -> ^@^MV ^L< 0.14.12.99.128.216 -> 0x000e0c6380d8 [snip]
As you can see, the "value" for a couple of the keys is garbled. These values are always garbled when the script runs, and the values are always garbled even when the same key and value results from a query on a different switch. This confuses me. When I query one of the OIDs directly which have the corrupt value, it looks OK:
$ snmpget -v 1 -c public 3c39t1-01 1.3.6.1.2.1.17.4.3.1.1.0.13.86.32.1 +2.60 SNMPv2-SMI::mib-2.17.4.3.1.1.0.13.86.32.12.60 = Hex-STRING: 00 0D 56 2 +0 0C 3C
My first instinct is that I am doing something wrong. Can anyone see what it is?

Thank you for your time.

Replies are listed 'Best First'.
Re: Net::SNMP result oddity
by glide (Pilgrim) on Jul 27, 2007 at 15:47 UTC
      You are correct, the unpack handles it correctly.

      Thank you!

        See also: http://rt.cpan.org/Public/Bug/Display.html?id=1946 for discussion as to what is really going on here.
Re: Net::SNMP result oddity
by McDarren (Abbot) on Jul 27, 2007 at 15:22 UTC
    Hi,

    I don't have an SNMP-enabled device handy that I can test your code on, and I don't see anything obviously wrong with a cursory look.... however, when I encounter problems like this I generally reach for Data::Dumper.

    Try inserting a..

    print Dumper($result);
    just before your first foreach loop, and then a..
    print Dumper($macTable);
    just before your second.

    This should be enough to tell you whether the data is coming back mangled, or if it's your hash assignments doing the mangling.

    Hope this helps,
    Darren :)

Re: Net::SNMP result oddity
by traveler (Parson) on Jul 27, 2007 at 15:41 UTC
    Your code is very similar to and probably based on Query MAC address from 3com switch (final solution). Note that the author of that code used printf. Try using printf and see if the numbers come out correctly. You may have to use a numeric printf conversion code.

    HTH, --traveler

Re: Net::SNMP result oddity
by gt2847c (Initiate) on Jul 29, 2007 at 03:32 UTC

    Not certain if you're running into the same problem I had when trying to generate an ARP table since the devices I'm trying to query don't have the BridgeMIB extensions on them... Just in case it is, try doing this:

    	$textMAC = unpack( "H*", $binMAC );
    

    where $binMAC is what the SNMP get gives you and $textMAC is the MAC address in string format.

    In your code, you might do:

    print "$key -> " . unpack( "H*", $macTable{$key}) . "\n";
    

    I also prefer the Cisco style presentation for the MAC addresses rather than the dashed approach, so, if you like the xxxx.xxxx.xxxx output, you can also do this:

    	$macaddr = substr( $macTable{$key}, 0, 4 ) . "." . 
    		substr( $macTable{$key}, 4, 4 ) .  "." . 
    		substr( $macTable{$key}, 8, 4 );
    

    My reasoning is that the snmpget you specified at the very end returns Hex-STRING but the text looks ok. The trick behind it is that the snmpget program is doing the conversion and presenting a text string, not the raw data returned from the get.

Re: Net::SNMP result oddity
by andyford (Curate) on Jul 27, 2007 at 17:44 UTC

    Your code works perfectly against a Cisco 3500.

    non-Perl: Andy Ford

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://629120]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-20 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found