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

Oh worthy monks, I need help with this script. I have used the base code from the Net::SNMP description from CPAN and have modified it to take an interface index number and via an snmp get, get the interface description.
The script works good if I hard code the node and the interface, but if I try to pull the variables from another source and use the array the script bombs saying "Result ERROR:: No response from remote host 'MyNode'."
#! c:/perl/bin/perl -w use strict; use Net::SNMP; ############################### Declarations ####################### my $now = localtime(); # Time in a readable format my $logfile = "D:/perl_logs/IfIndex2IfDesc.out"; # For Troublesho +oting open (logfile,">>",$logfile) or die "Failed to open IfIndex2IfDesc + log file: $!"; print logfile "\n"; print logfile "************ Start of script :$now ************** +****\n"; #Create an array with the data passed through. Seperate & count indiv +idually my $count = 0; foreach $element (@ARGV) { print logfile "$count,$element\n"; $count += 1; } my $community = "public"; $node = "MyNode"; $interface = "25"; # $node = ("$ARGV[1]"); # $interface = ("$ARGV[3]"); chomp ($node); chomp ($interface); my ($session, $error) = Net::SNMP->session( -hostname => shift || $node, -community => shift || $community ); print logfile "node:'$node'\n"; print logfile "IF:'$interface'\n"; if (!defined($session)) { printf("Session ERROR: %s.\n", $error); exit 1; } my $IfDescr = '.1.3.6.1.2.1.2.2.1.2.'.$interface; my $result = $session->get_request( -varbindlist => [$IfDescr] ); if (!defined($result)) { printf("Result ERROR:: %s.\n", $session->error); $session->close; exit 1; } printf("Interface Description / Alias for host '%s' is '%s'\n", $session->hostname, $result->{$IfDescr} ); print logfile ("Interface Description / Alias for host '$node' is ' +$result->{$IfDescr}'\n", ); print logfile "\n"; $session->close; close logfile; exit 0;

Still a newbie at this and would appreciate the help.
Thank you
JB

Replies are listed 'Best First'.
Re: Problem with adding variable from array
by Joost (Canon) on Feb 01, 2007 at 22:41 UTC
    This stuff:
    $node = ("$ARGV[1]"); $interface = ("$ARGV[3]");
    note that $ARGV[1] is the second command line argument to the script and $ARGV[3] is the fourth. Since you don't mention anything about how you're supplying the arguments the problem is probably there somewhere.

    Also note that you usually don't want to quote array lookups, it will work here since the values are strings.

    In my opinion

    $node = $ARGV[1]; $interface = $ARGV[3];
    is cleaner.

    Update: if you're wondering what's going on adding something like this just behind the argument assignment might help:

    warn "Supplied arguments: ",join(",",map { "'$_'" } @ARGV); warn "Node is '$node', interface is '$interface'.";
Re: Problem with adding variable from array
by GrandFather (Saint) on Feb 01, 2007 at 22:47 UTC

    There is some wierd stuff going on in:

    # $node = ("$ARGV[1]"); # $interface = ("$ARGV[3]"); chomp ($node); chomp ($interface); my ($session, $error) = Net::SNMP->session( -hostname => shift || $node, -community => shift || $community

    The commented out code implies you are picking up the second and fourth parameters on the command line, but the session call pulls the first and second parameters off the command line. Perhaps you could try something simple like:

    perl -e "print $ARGV[0]" first

    which should print first when run from the command line as a sanity check?


    DWIM is Perl's answer to Gödel
Re: Problem with adding variable from array
by Bennco99 (Acolyte) on Feb 01, 2007 at 23:39 UTC
    So I changed
    $node = $ARGV[1]; $interface = $ARGV[3]; and added warn "Supplied arguments: ",join(",",map { "'$_'" } @ARGV); warn "Node is '$node', interface is '$interface'."; and print logfile " arg0 '$ARGV[0]'\n"; print logfile " arg1 '$ARGV[1]'\n"; print logfile " arg2 '$ARGV[2]'\n"; print logfile " arg3 '$ARGV[3]'\n"; and I get the following output in the logfile 0,MyNode 1,MyNode 2,25 3,25 arg0 'MyNode' arg1 'MyNode' arg2 '25' arg3 '25' node:'MyNode' IF:'25' and on console Supplied arguments: '25','25' at IfIndex_to_IfDesc_v2.pl line 44. Node is 'MyNode', interface is '25'. at IfIndex_to_IfDesc_v2.pl line 45. Net::SNMP=HASH(0x184bce8)Result ERROR:: No response from remote host ' +MyNode'.
    Thank you
    JB

      Have you also removed those superfluous shifts in

      -hostname => shift || $node, -community => shift || $community

      i.e. changed those lines to

      -hostname => $node, -community => $community,

      (Otherwise, -community would most likely be set to "MyNode" (=$ARGV[1]), not "public"...)

        Removing the shifts worked. Thank you to everyone for your help!
        Thank you
        JB