#!/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_CONTEXT | BER_CONSTRUCTOR | 0x04 ], [ IpAddress => $STRING, BER_APPLICATION | 0x00 ], [ Counter => $INTEGER, BER_APPLICATION | 0x01 ], [ Gauge => $INTEGER, BER_APPLICATION | 0x02 ], [ TimeTicks => $INTEGER, BER_APPLICATION | 0x03 ], [ Opaque => undef, BER_APPLICATION | 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{'ent_OID'}, IpAddress => \$trap{'agentaddr'}, INTEGER => \$trap{'generic_trap'}, INTEGER => \$trap{'specific_trap'}, TimeTicks => \$trap{'timeticks'}, 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{'specific_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" }