Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Getting readable results from Net::SNMP get_table method

by fingers (Acolyte)
on May 16, 2001 at 03:25 UTC ( #80748=perlquestion: print w/replies, xml ) Need Help??

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

I am trying write a script that will poll cisco gear for information regarding it's CDP neighbors.

I had pretty much completed the project using telnet to get the information I wanted when I realized it would be quicker and less intrusive to gather this info with SNMP. I tracked down the appropriate oid and am using the Net::SNMP get_table method. It seems to be working, but I am getting output that looks like this:

HASH(0x82753ec)

What do I need to do to extract some meaningful information from this?

I have read the docs on the Net::SNMP module and am feeling very obtuse right now. I would love to see some actual examples of the get_table method if anyone has any existing code that uses it.

This is the pretty bare bones code I am using to test this method.
#!/usr/bin/perl -w use Net::SNMP; # requires a hostname and a community string as its arguments ($session,$error) = Net::SNMP->session(Hostname => $ARG[0], Community +=> $ARGV[1]); die "session error: $error" unless ($session); #1.3.6.1.4.1.9.9.23.1.2.1.1 is the OID that the CDP info starts at $result = $session->get_table("1.3.6.1.4.1.9.9.23.1.2.1.1"); die "request error: ".$session->error unless (defined $result); print "$result \n"; $session->close;

Thanks in advance on any light you can shine on this. I do believe that my eyes will start bleeding if I stare at any more docs.

Replies are listed 'Best First'.
(Ovid) Re: Getting readable results from Net::SNMP get_table method
by Ovid (Cardinal) on May 16, 2001 at 03:32 UTC
    Data::Dumper is a quick and easy method of seeing what's really in variables. In this case, the scalar $result appears to be a reference to an anonymous hash. Try this:
    use Data::Dumper; print Dumper( $result );
    That will let you see what you're getting back.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Getting readable results from Net::SNMP get_table method
by srawls (Friar) on May 16, 2001 at 03:36 UTC
    Whenever you see code like this: HASH(0x82753ec) then you are referencing the entire hash, this is obviosly not what you want, so try this:
    print "$result{$_}\n" for keys $result;
    I am not familiar with the module you are using, but I think this code should work; if you have any more questions - just ask.

    UPDATE: Thanks Ovid, I missed that one.

    The 15 year old, freshman programmer,
    Stephen Rawls

        When I use
        print "$result->{$_}\n" for keys %$result;
        I get output that looks like
        HASH(0x82a08e0)->(1.3.6.1.4.9.9.23.1.2.1.1.8.4.2)
        when I use the Data::Dumper module you suggest I get results that look like.
        '1.3.6.1.4.9.9.23.1.2.1.1.8.4.2' => 'WS-C6509'
        I assume that the oid is the key and the WS-C6509 is the object in the hash.

        How would I pull that all into hash that I would be able to manipulate?
Accessing a value without knowing the key
by fingers (Acolyte) on May 16, 2001 at 06:59 UTC
    Now that I can print out the entire hash and I know that I'm getting the data that I eventually want to use. I am stuck at trying to figure out how to access values in my hash without knowing exactly what the keys are.

      I am stuck at trying to figure out how to access values in my hash without knowing exactly what the keys are.

      @values = values %hash;

Re: Getting readable results from Net::SNMP get_table method
by Anonymous Monk on Feb 16, 2017 at 13:46 UTC

    after giving oid can i use grep in get_table

Re: Getting readable results from Net::SNMP get_table method
by mr.nick (Chaplain) on May 16, 2001 at 05:02 UTC
    (deleted)

    Bzzzt! Sorry, I messed up. I'm not sure what I was thinking.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://80748]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2020-08-15 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (78 votes). Check out past polls.

    Notices?