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

Hello. I have a small script I'm writing that will simply query for the hostname of a Cisco switch. This is my code:
#!/usr/bin/perl use Net::SNMP; my $host = "192.168.0.49"; my $community = "eX0gyN37"; my ($snmp,$snmperror) = Net::SNMP->session( -hostname => $host, -community => $community ); # hostname OID (this line doesnt work) # my $OID = "1.3.6.1.4.1.9.2.1.3"; # number of interfaces OID (this line works) my $OID = "1.3.6.1.2.1.2.1.0"; my $result = $snmp->get_request ( $OID ) or die "FAIL: $! \n"; print $result->{$OID} . "\n";
As you can see, I'm also using an OID that will display the number of interfaces on the switch. When I run the script as is, it works, and the number "53" is output (and is correct). However, if I comment the working line, and uncomment the other line, the script fails with the message:

FAIL: Transport endpoint is not connected

I am sure that the nonworking OID is correct, as you can see from the following result on the command line.

$ snmpwalk -v2c -c eX0gyN37 192.168.0.49 1.3.6.1.2.1.2.1.0 IF-MIB::ifNumber.0 = INTEGER: 53 $ snmpwalk -v2c -c eX0gyN37 192.168.0.49 1.3.6.1.4.1.9.2.1.3 SNMPv2-SMI::enterprises.9.2.1.3.0 = STRING: "sw-studentcenter-basement +-1"
What am I doing wrong? Why does the script work for one OID and not the other? The one and only difference I can see immediately is that the non-working OID has a "STRING" result on the command line, as opposed to the working "INTEGER" OID. Any suggestions? Thanks!

Replies are listed 'Best First'.
Re: SNMP Error using Net::SNMP - Transport endpoint is not connected
by NetWallah (Canon) on Apr 07, 2008 at 23:16 UTC
    If you notice, the response to the request for OID "192.168.0.49 1.3.6.1.4.1.9.2.1.3" ends in ...3.0 .

    This is an indication that you are supplying an incomplete OID to the SNMP query.

    The "SNMPWALK" request succeeds because it dives deeper, returning all child OID's. A simpler SNMPGET would have failed.

    Try changing the OID to "1.3.6.1.4.1.9.2.1.3.0" in your code.

    To keep this on-topic (perl-related), please do use strict;, etc, as arc_of_descent recommends. Also, it is a good idea to check return values, to ensure that your SNMP object did indeed get created.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

      yep, i realized that i had been using snmpwalk from the command line, and thats why it wasnt working. thanks for the input though!
      Hi,
      when the OID don't end with a index (it's not a table) you need to add a .0 in the end of the OID, to retrieve (or set) a value.

      see more in the rfc-1157

Re: SNMP Error using Net::SNMP - Transport endpoint is not connected
by arc_of_descent (Hermit) on Apr 07, 2008 at 22:17 UTC
    At first glance, I'd recommend you try a
    use strict; use warnings;

    at the start of your script. You never know!

    --
    Rohan