Use SnmpSource component to Generate V1 Trap.
To use this code sample, add a reference to the following
assemblies:
SnmpSource.dll
Then import the following namespaces at the top of your code file with
using
(for C#) or Imports
(for
VB) keywords:
SnmpSource
System.Net.sockets
System.Net
[C#]
//create snmp pdu
SnmpPdu pdu = new SnmpPdu();
//Specify the Pdu type
pdu.PduType = EnumPduType.TrapV1Pdu;
//Set the community string
pdu.Community = "public";
pdu.Version = SnmpVersion.Version1;
//Trap sample value
pdu.V1Enterprise = "1.3.6.1.4.1.12345.1";
pdu.V1remoteipaddress = "127.0.0.1";
pdu.V1GenericTrap = SnmpV1TrapType.coldStart;
SnmpVariable var = SnmpVariable.CreateSnmpVariable("1.3.6.1.4.1.12345.1.1", 123, SnmpVarType.INT);
pdu.AddRequestVariables(var);
//send the trap
byte[] trapdata = pdu.Encoding();
UdpClient client = new UdpClient("127.0.0.1", 162);
client.Send(trapdata,trapdata.Length);
client.Close();
[Vb]
'create snmp pdu
Dim pdu As New SnmpPdu
'Specify the Pdu type
pdu.PduType = EnumPduType.TrapV1Pdu
'Set the community string
pdu.Community = "public"
pdu.Version = SnmpVersion.Version1
'Trap sample value
pdu.V1Enterprise = "1.3.6.1.4.1.12345.1"
pdu.V1remoteipaddress = "127.0.0.1"
pdu.V1GenericTrap = SnmpV1TrapType.coldStart
Dim var As SnmpVariable = SnmpVariable.CreateSnmpVariable("1.3.6.1.4.1.12345.1.1", 123, SnmpVarType.INT)
pdu.AddRequestVariables(var)
'send the trap
Dim buffer1 As Byte() = pdu.Encoding()
Dim client As New UdpClient("127.0.0.1", 162)
client.Send(buffer1,buffer1.Length)
client.Close()
The syntax to use the included command line example.
Syntax: trapv1generator [Options] host enterprise agentip generictrap specifictrap timeticks [OID type value] ...
Options:
/port:portnumber or /p:portnumber Specify the port number. /community:community or /c:community Set community. /? or /help Display this usage message.
Example: trapv1generator /p:162 /c:public 127.0.0.1 1.3.6.1.4.1.12345.1 192.168.0.1 0 1234 0 1.3.6.1.4.1.12345.1.1 2 123
Copyright