Bernard wrote:
Hi,

Hello,

When I try to get the arp table from any type of device (oid 1.3.6.1.2.1.4.22.1.3) with the snmp_util module, it returns the 6 character, instead of the hex byte value.

How can I convert a 6 byte word into the hex equivalent like 00:0a:8b:bd:62:8a.

$ perl -le'
my $data = "\x00\x0a\x8b\xbd\x62\x8a";
( my $new = unpack "H*", $data ) =~ s/(..)(?=..)/$1:/g;
print $new;
'
00:0a:8b:bd:62:8a


Any help greatly appreciated

#!/usr/bin/perl

You should let perl help you too!

use warnings;
use strict;


use SNMP_util;


my $IPADDR = $ARGV[0] || die "Oops, no ipaddr specified....";
my $COMM = $ARGV[1] || die "Oops, no community specified....";
my $OID = $ARGV[2] || die "Oops, no oid specified....";

Although that *will* technically work, you are using the wrong logical operator. The logical or operator (||) has higher precedence than the assignment operator (=).

The usual idiom is to use '||' for assignment:

my $var = $ARGV[0] || 'some default value';

And to use 'or' for flow control:

my $var = $ARGV[0] or die "Opps\n";


print("Getting info for $IPADDR with comm $COMM ($OID)\n");

$HOST = $COMM . "@" . $IPADDR;
$SNMPVER = "1";
$SNMPSTR = "$HOST" . ":::::" . "$SNMPVER";
print("Getting table.\n");

$length = length($OID);
$oid = $OID;
   $oid = substr($oid,0,$length);

Why not simply:

my $oid = substr $OID, 0, length $OID;

Which is just a long way of saying:

my $oid = $OID;


print "Trying 'getnext' on $host with $oid\n";
while ( substr($oid,0,$length) eq $OID ) {
 my @ret = &snmpgetnext($SNMPSTR, "$oid");

You shouldn't prefix a subroutine name with an ampersand.

perldoc perlsub


 foreach my $desc (@ret) {
   ($oid, $desc) = split(':', $desc, 2);
   $testStr = substr($oid,0,$length);
   if ( substr($oid,0,$length) eq $OID ) {
     print "$oid = $desc ($testStr)')\n";
   }
 }
}



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to