Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Looking for a simple way to just send an SNMP V1 Trap

by w3ntp (Beadle)
on Jun 11, 2007 at 14:26 UTC ( #620498=perlquestion: print w/replies, xml ) Need Help??

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

I looking for a simple SNMP plug in module for perl that allows me to send a SNMP V1 trap without having to load all the cryptographic prereqs that net::SNMP requires downloadable from CPAN. any Sugestions? thanks w3ntp
  • Comment on Looking for a simple way to just send an SNMP V1 Trap

Replies are listed 'Best First'.
Re: Looking for a simple way to just send an SNMP V1 Trap
by quester (Vicar) on Jun 11, 2007 at 22:32 UTC
    Look at "SNMP support for Perl 5" in This is the same SNMP interface parkace used by MRTG.

    For a quick example, adapted from the documentation for snmptrap on that page,

    # use warnings; # use strict; my $version=1; use lib '/usr/lib/mrtg2'; # location of and use SNMP_Session; use BER; my $trap_receiver = ""; my $trap_community = "SNMP_Traps"; my $trap_session = $version eq '1' ? SNMP_Session->open ($trap_receiver, $trap_community, 162) : SNMPv2c_Session->open ($trap_receiver, $trap_community, 162); my $myIpAddress = ""; my $start_time = time; sub link_down_trap ($$) { my ($if_index, $version) = @_; my $genericTrap = 2; # linkDown my $specificTrap = 0; my @ifIndex_OID = ( 1,3,6,1,2,1,2,2,1,1 ); # UPDATE: not ifIn +dexOID my @ifDescr_OID = ( 1,3,6,1,2,1,2,2,1,2 ); # UPDATE: added my $upTime = int ((time - $start_time) * 100.0); my @myOID = ( 1,3,6,1,4,1,2946,0,8,15 ); warn "Sending trap failed" unless ($version eq '1') ? $trap_session->trap_request_send (encode_oid (@myOID), encode_ip_address ($myIpAd +dress), encode_int ($genericTrap), encode_int ($specificTrap) +, encode_timeticks ($upTime) +, [encode_oid (@ifIndex_OID, +$if_index), encode_int ($if_index)], [encode_oid (@ifDescr_OID, +$if_index), encode_string ("foo")]) : $trap_session->v2_trap_request_send (\@linkDown_OID, $up +Time, [encode_oid (@ifInd +ex_OID,$if_index), encode_int ($if_in +dex)], [encode_oid (@ifDes +cr_OID,$if_index), encode_string ("fo +o")]); } link_down_trap 123, $version;

    UPDATE:I fixed the definition of ifIndexOID to ifIndex_OID and added the definition of ifDescr_OID. Sorry, I thought I was copying a working example, but evidently it wasn't.

    Perversely, without correct definitions the program still runs with no warnings, even with warnings and strict enabled, and it sends an snmptrap packet... but the oid=value pairs in the packet are not valid.

    In order to debug this sort of problem, you can send the snmptrap packets to a system running Wireshark or a Unix system running snmptrapd. But be aware that the NET-SNMP version 5.0.9 version of snmptrapd won't log anything at all for a badly formatted trap packet, unless you include you include the hyperverbose -D (debug) option.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://620498]
Approved by varian
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-05-17 12:20 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.