snmpv3getone.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:3k
- /* $Id: snmpv3getone.java,v 1.1 2002/06/15 14:42:11 ram Exp $ */
- /*
- * @(#)snmpv3getone.java
- * Copyright (c) 1999 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 do
- * the basic SNMP operation GET using com.adventnet.snmp.snmp2 package of
- * AdventNetSNMP api.
- *
- * The user could run this application by giving the following usage.
- *
- * java snmpv3getone hostname OID [OID]
- *
- * where
- *
- * hostname is the RemoteHost (agent).The Format is string without double qoutes/IpAddress.
- * OID is the Object Identifier. Multiple OIDs can also be given.
- * The entire OID can be given or it can be given in the form of 1.1.0.
- * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
- * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 .
- *
- * Example usage:
- *
- * java snmpv3getone adventnet 1.1.0 1.2.0 1.3.0 1.4.0
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.snmp2.usm.*;
- public class snmpv3getone {
- public static void main(String args[]) {
-
- if( args.length < 2)
- {
- System.out.println("Usage : java snmpv3getone hostname OID");
- System.exit(0);
- }
-
- // get the hostname and the OID
- String remoteHost = args[0];
- String OID = args[1];
-
- String userName = "testuser";
- int authProtocol = USMUserEntry.MD5_AUTH;
- int USM_SECURITY_MODEL = 3;
- String authPassword = "test";
- String privPassword = "test";
- String contextName = "testmachine";
- String contextID = "testing";
-
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- api.setDebug( true );
-
- SecurityProvider provider = api.getSecurityProvider();
- provider.registerSecurityModel(3,"com.adventnet.snmp.snmp2.usm.USMUserTable","com.adventnet.snmp.snmp2.usm.USMUserEntry");
- try
- {
- provider.createTable(3);
- }
- catch(SnmpException e)
- {
- System.out.println("Exception in creating Security Table for USM");
- }
-
- // Open session
- SnmpSession session = new SnmpSession(api);
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- //Build GET Request PDU
- SnmpPDU pdu = new SnmpPDU();
- //get the value from the command line
- UDPProtocolOptions option = new UDPProtocolOptions(remoteHost);
- pdu.setProtocolOptions(option);
- // port number of the remote agent
- option.setRemotePort(8001);
- // SNMP version
- session.setVersion(3);
- pdu.setCommand(SnmpAPI.GET_REQ_MSG);
-
- pdu.setSecurityModel(USM_SECURITY_MODEL);
- pdu.setUserName(userName.getBytes());
- USMUtils.init_v3_params(userName, authProtocol, authPassword, privPassword, session.getPeername(), session.getRemotePort(),session);
- pdu.setContextName(contextName.getBytes());
- pdu.setContextID(contextID.getBytes());
- // add OIDs
- for (int i=1; i<args.length; i++)
- {
- SnmpOID oid = new SnmpOID(args[i]);
- pdu.addNull(oid);
- }
- try
- {
- // Send PDU and receive response PDU
- SnmpPDU result = session.syncSend(pdu);
- }
- catch (SnmpException e)
- {
- System.err.println("Error sending SNMP request: "+e);
- }
-
- // print the response pdu varbinds
- System.out.println(result.printVarBinds());
-
- // close session
- session.close();
-
- // close the api thread
- api.close();
-
- }
- }