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

I wrote a simple perl program to get cisco device's sysuptime with Net::Snmp module.And it don't work normal.

I use sniffer to get the trace,the trace is like this:

99.140.12.9---send snmp get_request---->99.1.64.65
99.1.64.65----send snmp get_response--->99.140.12.9
99.140.12.9---send icmp port unreachable-->99.1.64.65
So the question is why it don't receive the response ?
thanks!
this is my program: #!/usr/bin/perl use strict; use lib '../lib'; use Net::SNMP; my ($session, $error) = Net::SNMP->session( -hostname => '99.1.64.65', -community => '12345', -port => 161 ); if (!defined($session)) { printf("snmp::session ERROR: %s.\n", $error); exit 1; } my $sysUpTime = '1.3.6.1.2.1.1.3.0'; my $result = $session->get_request( $sysUpTime ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } printf("sysUpTime for host '%s' is %s\n", $session->hostname, $result->{$sysUpTime} ); $session->close;

Replies are listed 'Best First'.
Re: strange snmp question
by insaniac (Friar) on Oct 25, 2004 at 08:21 UTC
    hey

    try working with get_next_request() .. it works better ;-)
    i'll show sample example which (first of all works) I use to check the memory on cisco routers:

    #!/usr/local/bin/perl # use Net::SNMP qw(oid_lex_sort oid_base_match SNMP_VERSION_1 DEBUG_ALL +); use strict; my $ip = scalar @ARGV >= 1 ? $ARGV[0] : "192.168.0.1"; my $community = scalar @ARGV == 2 ? $ARGV[1] : "secret"; my ($s,$e)=Net::SNMP->session(-hostname=> $ip, -community =>$community +); if (!defined($s)) { printf("error: %s.\n", $e); exit 1; } print "-"x78,"\n"; print "Search memoryPools for: $ip (community: $community)\n"; print "-"x78,"\n"; my $oid = ".1.3.6.1.4.1.9.9.42.1.1.11"; my @args = ( -varbindlist => [$oid]); while (defined($s->get_next_request(@args))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($oid, $_)) { last; } printf("rttMonApplFreeMemLowWaterMark: %s\n", $ +s->var_bind_list->{$_}); @args = (-varbindlist => [$_]); } $oid = ".1.3.6.1.4.1.9.9.48.1.1.1.6"; my @args = ( -varbindlist => [$oid]); while (defined($s->get_next_request(@args))) { $_ = (keys(%{$s->var_bind_list}))[0]; if (!oid_base_match($oid, $_)) { last; } printf("ciscoMemoryPoolFree: %s\n", $ +s->var_bind_list->{$_}); @args = (-varbindlist => [$_]); } print "-"x78,"\n"; $s->close();

    hope this helps...
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame
      thanks for your reply. My program is running normal on other linux machine.So I think the program don't have bug.
        aha... maybe - just guessing now- there are some ACLs created on the cisco router, which disallow your IP or the network you're in to query the router. I mean: this seems logical if your script works on other hosts. have you tried the snmpwalk or snmpget command line tools? i always do this to check if the community string is correct and to see if i get any replies back...
        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame
Re: strange snmp question
by Anonymous Monk on Oct 25, 2004 at 09:33 UTC
    Hi, insaniac

    thanks for your reply.You can see from the trace, 99.140.12.9 does reply "icmp port unreachable " to 99.1.64.65. So I think there isn't any acl between the machines.

    I tried snmpget ,and get same result.Maybe the linux has some problem.

    I will try to solve the problem. thanks again

      No sweat man... hey, maybe a router/firewall is denying your ICMP replies towards that specific router? try finding a router/device which you CAN query over SNMP (maybe on the same network as the host you're performing the query from)..

      good luck and happy hacking! :-D
      --
      to ask a question is a moment of shame
      to remain ignorant is a lifelong shame

        Hi insaniac,

        I got the answer.There is firewall installed on the linux machine.So it is deny all the incoming udp (snmp)packet.

        After I use "/sbin/iptables -F" to stop the firewall.It is work normal.

        thanks!