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

When using the perl snmp module, I want to disable the printing of printable and control characters and print the hex value "(i.e. 0x0a prints LF) For now, I substituted the LF char with 0x000a as indicated in the below code. Also included is an excerpt from the man page.
$MAIN::a = 1; $stuff='c:\scripts\ipListPart3.txt'; open STUFF, $stuff or die "Cannot open $stuff for read :$!"; while (<STUFF>) { chomp $_; $MAIN::host = $_; my $out="c:/scripts/out.txt"; open OUT, ">>$out" or die "Cannot open $out for write :$!"; printf OUT ("\n$MAIN::host\n"); $MAIN::counter = 0; $stuff1='c:\scripts\mibs1a.txt'; open STUFF1, $stuff1 or die "Cannot open $stuff1 for read :$!"; while (<STUFF1>){ chomp $_; $MAIN::oid = $_; my $out="c:/scripts/out.txt"; open OUT, ">>$out" or die "Cannot open $out for write :$!"; @MAIN::names = ("adminStatus","operStatus","autoNeg","adminSpeed","adm +inDuplex" ,"operSpeed","operDuplex","taggingType","vids","discardTagged", "discardUntagged","defaultVid","stg1Enable","stg1State","stg1Faststart +"); printf OUT ("$MAIN::names[$MAIN::counter]\t"); $MAIN::counter++; for ($MAIN::a = 1; $MAIN::a <= 48; $MAIN::a++){ &subOne; } sub subOne { $_ = $MAIN::oid; $_ =~ s/port/$MAIN::a/; use Net::SNMP; my ($session, $error) = Net::SNMP->session( -hostname => shift || $MAIN::host, -community => shift || 'public', -port => shift || 161, -timeout => shift || 5 ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $DesignatedRoot = $_; my $result = $session->get_request( -varbindlist => [$DesignatedRoot] ); if (!defined($result)) { printf("ERROR: %s.\n", $session->error); $session->close; exit 1; } my $out="c:/scripts/out.txt"; open OUT, ">>$out" or die "Cannot open $out for write :$!"; $a = ($result->{$DesignatedRoot}); $a =~ s/\n/0x000a/; printf OUT $a; #printf OUT ($result->{$DesignatedRoot}); printf OUT ("\t"); if ($MAIN::a == 48) { printf OUT ("\n"); } $session->close; } } } exit 0;
Excerpt: When the object decodes the GetResponse-PDU that is returned in response to a SNMP message, certain values are translated into a more "human readable" form. By default the following translations occur: * OCTET STRINGs and Opaques containing non-printable ASCII characters are converted into a hexadecimal representation prefixed with "0x". NOTE: The following ASCII control characters are considered to be printable by the module: NUL(0x00), HT(0x09), LF(0x0A), FF(0x0C), and CR(0x0D). * TimeTicks integer values are converted to a time format. * NULL values return the string "NULL" instead of an empty string. * noSuchObject exception values return the string "noSuchObject" instead of an empty string. If translation is not enabled, the SNMP error-status field is set to 128 which is equal to the exported definition NOSUCHOBJECT (see "EXPORTS"). * noSuchInstance exception values return the string "noSuchInstance" instead of an empty string. If translation is not enabled, the SNMP error-status field is set to 129 which is equal to the exported definition NOSUCHINSTANCE (see "EXPORTS"). * endOfMibView exception values return the string "endOfMibView" instead of an empty string. If translation is not enabled, the SNMP error-status field is set to 130 which is equal to the exported definition ENDOFMIBVIEW (see "EXPORTS"). * Counter64, Counter, Gauge, and TimeTick values that have been incorrectly encoded as signed negative values are returned as unsigned values.

2006-07-31 Retitled by Corion, as per Monastery guidelines
Original title: 'SNMP'

  • Comment on Disable the printing of printable and control characters with Net::SNMP (was: SNMP)
  • Download Code

Replies are listed 'Best First'.
Re: Disable the printing of printable and control characters with Net::SNMP (was: SNMP)
by rodion (Chaplain) on Jul 31, 2006 at 12:31 UTC
    I'm not quite sure what you're asking for. If you just want to print out all the values of $a as their hex values, then
    my @bin = unpack 'C*', $a; $a = ''; for (@bin) { $a .= sprintf ' 0x%02x', $_; }
    should do it. The coding style is pretty hard to get through (and you can get some pointers from the monks here if you'd like), but one thing that stands out is that you open "OUT" twice, without closing it.

    Update: Woops, fixed unpack spec.

      Trouble is that control characters are returned. Thanks anyway, I've contacted the Net::SNMP module author who suggests disabling the internal translation and performing my own. This was his response Michael, Look at a simliar issue reported at: http://rt.cpan.org/Public/Bug/Display.html?id=18699