- /* $Id: SnmpSendTrap_one.java,v 1.1 2002/06/15 14:42:11 ram Exp $ */
- /*
- * @(#)SnmpSendTrap_one.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is a tutorial example program to explain how to write an application to send a
- * v1 Trap message using com.adventnet.snmp.snmp2 package of AdventNetSNMP2 api.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpSendTrap hostname
- *
- * where
- *
- * hostname is the RemoteHost (agent).The Format is string without double qoutes/IpAddress.
- *
- *
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SnmpSendTrap_one {
- public static void main(String args[]) {
- if( args.length < 1)
- {
- System.out.println("Usage : java SnmpSendTrap_one hostname ");
- System.exit(0);
- }
- // Take care of getting the hostname
- String remoteHost = args[0];
- // Start SNMP API
- SnmpAPI api;
- api = new SnmpAPI();
- api.start();
- api.setDebug(true);
- // Open session
- SnmpSession session = new SnmpSession(api);
- // set remote host
- session.setPeername(remoteHost);
- // set remote port
- session.setRemotePort(8001);
- //open the session
- try {
- session.open();
- } catch (SnmpException e ) {
- System.err.println("Error opening socket: "+e);
- }
- // Build SNMPv1 Trap PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand( api.TRP_REQ_MSG );
- // fill in v1 trap PDU fields
- try {
- pdu.setEnterprise(new SnmpOID("1.2.0"));
- pdu.setAgentAddress(InetAddress.getByName("192.168.1.40"));
- pdu.setTrapType(0);
- pdu.setSpecificType(6);
- pdu.setUpTime(1000);
- }
- catch (Exception ex) {
- System.err.println("error in one or more required fields: "+ex);
- }
- // add OID
- SnmpOID oid = new SnmpOID("1.5.0");
- // set the type
- byte dataType;
- dataType = SnmpAPI.STRING;
- String value = "testing from simple program";
- // create SnmpVar instance for the value and the type
- SnmpVar var = null;
- try {
- var = SnmpVar.createVariable( value, dataType );
- }
- catch(SnmpException e){
- System.err.println("Cannot create variable: " + oid + " with value: " + value);
- return;
- }
- //create varbind
- SnmpVarBind varbind = new SnmpVarBind(oid, var);
- // add variable binding
- pdu.addVariableBinding(varbind);
- // send PDU
- try {
- session.send(pdu);
- } catch (SnmpException e) {
- System.err.println("Sending PDU"+e.getMessage());
- }
- System.exit(0);
- }
- }