SnmpTrapd_one.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:2k
- /* $Id: SnmpTrapd_one.java,v 1.1 2002/06/15 14:42:11 ram Exp $ */
- /*
- * @(#)SnmpTrapd_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 for receiving traps using
- * the com.adventnet.snmp.snmp2 package of AdventNetSNMP api.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpTrapd_one
- *
- * the application will start receiving traps in the port 8001.
- *
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SnmpTrapd_one {
- public static void main(String args[]) {
-
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- api.setDebug(true);
- // Open session
- SnmpSession session = new SnmpSession(api);
-
- // set local port
- SnmpPDU pdu = new SnmpPDU();
- UDPProtocolOptions option = new UDPProtocolOptions("localhost");
- pdu.setProtocolOptions(option);
- pdu.setLocalPort(8001);
- // set community
- session.setCommunity(null);
-
- // open the session
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- // wait for the trap pdu
- pdu = session.receive(0);
- System.out.println("Waiting to receive traps in the port "+pdu.getLocalPort() +".......");
-
- while (pdu == null)
- {
- pdu = session.receive(0);
- if(pdu != null)
- {
- if (pdu.getCommand() == SnmpAPI.TRP_REQ_MSG)
- {
- // print the received trap information
- System.out.println("Trap received from: "+pdu.getAddress() +", community: " + pdu.getCommunity());
- System.out.println("Enterprise: " + pdu.getEnterprise());
- System.out.println("Agent: " + pdu.getAgentAddress());
- System.out.println("TRAP_TYPE: " + pdu.getTrapType());
- System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
- System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
- System.out.println(pdu.printVarBinds());
- pdu = null;
- System.out.println("Continues waiting to receive traps in the port "+session.getLocalPort() +".......");
- }
- else
- {
- System.err.println("Non trap PDU received.");
- }
- }
- }
- }
- }