I'm using Convert::BER to write a simple SNMP Trap receiver (much like Mon::SNMP, which I can't seem to find a PPM for my ActiveState). Nonetheless, I have it working pretty good except for the varbinds.

My test is returning: Convert::BER=ARRAY(0x1a67994), as one of my variables. I assume I can iterate over this with a foreach (@{<VAR_HERE>}) loop, but I keep getting the Convert::BER=ARRAY reference. I inserted comments and a Data::Dumper line to help with my (and any kind Monk's) troubleshooting.

#!/usr/bin/perl use strict; use IO::Socket; use Convert::BER; use Convert::BER qw(/^(\$|BER_)/); my @traptypes = ("COLDSTART", "WARMSTART", "LINKDOWN", "LINKUP", " +AUTHFAIL", "EGPNEIGHBORLOSS", "ENTERPRISESPECIFIC"); my %trap; my $sock = IO::Socket::INET->new(LocalPort => '162', Proto => 'udp' ) || die "Cannot open port"; my $ber = new Convert::BER; $ber->define( [ Trap_PDU => $SEQUENCE, BER_CONTEX +T | BER_CONSTRUCTOR | 0x04 ], [ IpAddress => $STRING, BER_APPLIC +ATION | 0x00 ], [ Counter => $INTEGER, BER_APPLIC +ATION | 0x01 ], [ Gauge => $INTEGER, BER_APPLIC +ATION | 0x02 ], [ TimeTicks => $INTEGER, BER_APPLIC +ATION | 0x03 ], [ Opaque => undef, BER_APPLIC +ATION | 0x04 ] ); my $buf; while (1) { $sock->recv($buf, 1500); my ($port, $ipaddr) = sockaddr_in($sock->peername); my $host = inet_ntoa($ipaddr); $ber->buffer($buf); $ber->decode( SEQUENCE => [ INTEGER => \$trap{'version'}, STRING => \$trap{'community'}, Trap_PDU => [ OBJECT_ID => \$trap{'en +t_OID'}, IpAddress => \$trap{'ag +entaddr'}, INTEGER => \$trap{'ge +neric_trap'}, INTEGER => \$trap{'sp +ecific_trap'}, TimeTicks => \$trap{'ti +meticks'}, SEQUENCE => [ ANY => \$ +trap{'ber_varbindlist'} ], ], ] ); my $p = sprintf "%s\t%i\t%i\t%s\t%s\t%s\t%s\t%s\t%s\t", $host, + $port, $trap{'version'}, $trap{'community'}, $trap{'ent_OID'}, inet_ +ntoa($trap{'agentaddr'}), $traptypes[$trap{'generic_trap'}], $trap{'s +pecific_trap'}, $trap{'timeticks'}; # This is the value I need to expand print "varbind = $trap{'ber_varbindlist'}\n"; # This is how I'm trying to do it my %varbind; my ($oid, $val); foreach my $i (@{$trap{'ber_varbindlist'}}) { $ber->buffer($i); $ber->decode( SEQUENCE => [ OBJECT_ID => \$oid, ANY => \$val, ] ); $varbind{$oid} = $val } # This is dumping what I've expanded, but it isn't correct use Data::Dumper; print Dumper(%varbind); print "$p\n" }

Since the above is just a server (listener), I'm using another simple script to send the trap I want to analyze. The following code does NOT need to be analyzed, I just wanted to make it easy for any Monks who wanted to help out. I'm calling the following script with EXACTLY what follows:

C:\> snmptrap.pl localhost public arg1 arg2 arg3 arg4 arg5
# snmptrap.pl #! /usr/bin/perl use strict; use Net::SNMP qw(:ALL); my ($session, $error) = Net::SNMP->session( -hostname => $ARGV[0], -community => $ARGV[1], -port => SNMP_TRAP_PORT, # Need to use port 162 ); if (!defined($session)) { printf("ERROR: %s.\n", $error); exit 1; } my $srcip=$ARGV[2]; my $dstip=$ARGV[3]; my $srcport=$ARGV[4]; my $dstport=$ARGV[5]; my $personality=$ARGV[6]; my $result = $session->trap( -enterprise => '1.3.6.1.4.1.50000', -generictrap => 6, -specifictrap => 1, -varbindlist => [ '1.3.6.1.4.1.50000.1.1', OCTET_STRING, "$srcip", '1.3.6.1.4.1.50000.1.2', OCTET_STRING, "$dstip", '1.3.6.1.4.1.50000.1.3', OCTET_STRING, "$srcport", '1.3.6.1.4.1.50000.1.4', OCTET_STRING, "$dstport", '1.3.6.1.4.1.50000.1.5', OCTET_STRING, "$personality" ] ); $session->close(); exit 0;

In reply to Convert::BER printing values by VinsWorldcom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.