Net::SNMP and icmpEchoReq Assistance

C

csb

While searching for an answer to my problem, I came across this group.
Please let me know if it doesn't belong here and I'll continue my
search elsewhere.

I'm in the process of converting some shell scripts over to Perl.
I've always used Net::SNMP and it's served me without fail.
However, I'm beating my head against a wall with an issue and I
cannot figure out what I'm doing wrong.

I'm attempting to have my Perl script generate a remote ping
(icmpEchoReq) and display the response time. I currently perform this
function with 'snmpget' but I'd like it to not call external
files from Perl if at all possible.

When I perform the Net::SNMP Get, my $getresponse-> is empty. Using
Ethereal, I can see the SNMP Get request departing the target host, and

the response returning to the target host.

When I perform the same Get using 'snmpget' I display the response
properly. I suspect it has something to do with the Get and the
response to the Get not being the exact same OID. For instance, if I
use 'snmpget' and Get OID ".1.3.6.1.4.1.11.2.7.1.64.5.10.0.0.3"
(remote ping 10.0.0.3, packet size 64, timeout 5 seconds) the response
comes back as OID "1.3.6.1.4.1.11.2.7.1.0"
(hp.nm.icmp.icmpEchoReq.0).

Could this be the reason my $getresponse-> is null? How can I overcome

this using Net::SNMP?

All code and output are listed below. Any advice or direction would be

greatly appreciated.

Thanks!
Chuck

------------------------------------------
CODE (PERL): 'rping.pl'

use Net::SNMP;

$OIDicmpEchoReq1 = '1.3.6.1.4.1.11.2.7.1.64.5.10.0.0.3';
$OIDicmpEchoReq2 = '1.3.6.1.4.1.11.2.7.1.0';
$OIDifOperStatus = '1.3.6.1.2.1.2.2.1.8.1';

$Target = 'foo';
$Community = 'bar';
$Timeout = 10;
$Retries = 0;

#BEGIN

&do_snmpget($OIDicmpEchoReq1);
print "Query Response A: $retval\n";

# &do_snmpget($OIDicmpEchoReq2);
# print "Query Response B: $retval\n";

&do_snmpget($OIDifOperStatus);
print "Query Response C: $retval\n";

# END

sub do_snmpget {
($oid_to_get)=@_;
($session, $error) = Net::SNMP->session(
Timeout => $Timeout,
Retries => $Retries,
Hostname => $Target,
Community => $Community,
Port => 161,
Translate => [-all => 0x0]);
if (!defined($getresponse =
$session->get_request($oid_to_get))) {
$error_msg=$session->error; print "ERROR - Target:
$Target;
Cmty: $Community; $oid_to_get; $error_msg\n"; $session->close;}
#print "a: $getresponse->{$oid_to_get}\n";
$retval=$getresponse->{$oid_to_get}; $session->close;
#print "retval-a: $retval\n";
#$retval=unpack("A*",pack("H*",$retval)) if (retval =~ /^0x/);
#print "retval-b: $retval\n";
return($retval);}

-----------------------------------------
OUTPUT (PERL): 'perl rping.pl'

Query Response A:
Query Response C: 1

==========================================
CODE (SHELL): 'rping.sh'

snmpget -c bar foo .1.3.6.1.4.1.11.2.7.1.64.5.10.0.0.3
snmpget -c bar foo .1.3.6.1.2.1.2.2.1.8.1

------------------------------------------
OUTPUT (SHELL): 'rping.sh'

hp.nm.icmp.icmpEchoReq.0 : INTEGER: 22
interfaces.ifTable.ifEntry.ifOperStatus.1 : INTEGER: up

------------------------------------------
 
C

csb

It's amazing. I attempt to solve a problem for hours upon hours. I
finally post my query to a couple of newsgroups. I then sit back and
await some enlightened knowledge.

And, behold, the answer comes to me. Damnit!

I figured out that although I am performing a $session->get_request on
the full icmpEchoReq OID, I should be performing a
$getresponse->icmpEchoReq.0.

I modified my code (below) and everything works as I expect it to.

If you responded (or thought about it), thank you!

------------------------------------------
CODE (PERL): 'rping.pl'

use Net::SNMP;

$OIDicmpEchoReq = '1.3.6.1.4.1.11.2.7.1.64.5';
$OIDicmpEchoResp = '1.3.6.1.4.1.11.2.7.1.0';

$Target = 'foo';
$Community = 'bar';
$Timeout = 10;
$Retries = 0;

#BEGIN

$NodeTarget = '10.0.0.3';

$OIDQuery = "$OIDicmpEchoReq.$NodeTarget";

$Rping = 1;
&do_snmpget($OIDQuery);

print "Node Ping response: $retval\n";

# END

sub do_snmpget {
($oid_to_get)=@_;
if ($Rping) {$OIDGetRequest = $OIDicmpEchoResp; $Rping = "";}
else {$OIDGetRequest = $oid_to_get;}
($session, $error) = Net::SNMP->session(
Timeout => $Timeout,
Retries => $Retries,
Hostname => $Target,
Community => $Community,
Port => 161,
Translate => [-all => 0x0]);
if (!defined($getresponse = $session->get_request($oid_to_get))) {
$error_msg=$session->error; print "ERROR - Target: $Target;
Cmty: $Community; $oid_to_get; $error_msg\n"; $session->close;}
$retval=$getresponse->{$OIDGetRequest}; $session->close;
$retval=unpack("A*",pack("H*",$retval)) if (retval =~ /^0x/);
#print "retval-b: $retval\n";
return($retval);}


-----------------------------------------
OUTPUT (PERL): 'perl rping.pl'


Node Ping response: 115


==========================================
 
C

csb

It's amazing. I attempt to solve a problem for hours upon hours. I
finally post my query to a couple of newsgroups. I then sit back and
await some enlightened knowledge.

And, behold, the answer comes to me. Damnit!

I figured out that although I am performing a $session->get_request on
the full icmpEchoReq OID, I should be performing a
$getresponse->icmpEchoReq.0.

I modified my code (below) and everything works as I expect it to.

If you responded (or thought about it), thank you!

------------------------------------------
CODE (PERL): 'rping.pl'

use Net::SNMP;

$OIDicmpEchoReq = '1.3.6.1.4.1.11.2.7.1.64.5';
$OIDicmpEchoResp = '1.3.6.1.4.1.11.2.7.1.0';

$Target = 'foo';
$Community = 'bar';
$Timeout = 10;
$Retries = 0;

#BEGIN

$NodeTarget = '10.0.0.3';

$OIDQuery = "$OIDicmpEchoReq.$NodeTarget";

$Rping = 1;
&do_snmpget($OIDQuery);

print "Node Ping response: $retval\n";

# END

sub do_snmpget {
($oid_to_get)=@_;
if ($Rping) {$OIDGetRequest = $OIDicmpEchoResp; $Rping = "";}
else {$OIDGetRequest = $oid_to_get;}
($session, $error) = Net::SNMP->session(
Timeout => $Timeout,
Retries => $Retries,
Hostname => $Target,
Community => $Community,
Port => 161,
Translate => [-all => 0x0]);
if (!defined($getresponse = $session->get_request($oid_to_get))) {
$error_msg=$session->error; print "ERROR - Target: $Target;
Cmty: $Community; $oid_to_get; $error_msg\n"; $session->close;}
$retval=$getresponse->{$OIDGetRequest}; $session->close;
$retval=unpack("A*",pack("H*",$retval)) if (retval =~ /^0x/);
#print "retval-b: $retval\n";
return($retval);}


-----------------------------------------
OUTPUT (PERL): 'perl rping.pl'


Node Ping response: 115


==========================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top