Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
snmpget.java.txt
Package: AdventNetSNMPAPI_4.zip [view]
Upload User: aonuowh
Upload Date: 2021-05-23
Package Size: 35390k
Code Size: 6k
Category:
SNMP
Development Platform:
C/C++
- /* $Id: snmpget.src,v 1.4 2002/09/09 05:41:02 parasuraman Exp $ */
- /*
- * @(#)snmpget.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is an example program to explain how to write an application to do
- * the basic SNMP operation GET using com.adventnet.snmp.snmp2 package of
- * AdventNetSNMP2 api.
- * The user could run this application by giving any one of the following usage.
- *
- * java snmpget [options] hostname oid ...
- *
- * v1 request:
- * java snmpget [-d] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
- * e.g.
- * java snmpget -p 161 -c public adventnet 1.1.0 1.2.0
- *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only">
- * v2c request:
- * java snmpget [-d] [-v version(v1,v2)] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
- * e.g. For v1 request give -v v1 or drop the option -v .
- * java snmpget -p 161 -v v2 -c public adventnet 1.1.0 1.2.0
- *<img SRC="images/v3only.jpg" ALT="v3 only">
- * v3 request:
- * java snmpget [-d] [-v version(v1,v2,v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i context_id] host OID [OID] ...
- * e.g.
- * java snmpget -v v3 -u initial2 -w initial2Pass -a MD5 10.3.2.120 1.2.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 . You can also
- * give the entire OID .
- *
- * Options:
- * [-d] - Debug output. By default off.
- * [-c] <community> - community String. By default "public".
- * [-p] <port> - remote port no. By default 161.
- * [-t] <Timeout> - Timeout. By default 5000ms.
- * [-r] <Retries> - Retries. By default 0.
- *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version> - version(v1 / v2 / v3). By default v1.
- * [-u] <username> - The v3 principal/userName
- * [-a] <autProtocol> - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
- * [-w] <authPassword> - The authentication password.
- * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
- * [-n] <contextName> - The contextName to be used for the v3 pdu.
- * [-i] <contextID> - The contextID to be used for the v3 pdu.
- * host Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
- * OID Mandatory - Give multiple no. of Object Identifiers.
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.snmp2.usm.*;
- public class snmpget {
- public static void main(String args[]) {
- // Take care of getting options
- String usage = "snmpget [-d] [-v version(v1,v2,v3)] [-c community] [-p port] [-r retries] [-t timeout] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID] host OID [OID] ...";
- String options[] = { "-d", "-c", "-wc", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s", "-n", "-i"};
- String values[] = { "None", null, null, null, null, null, "None", null, null, null, null, null, null, null };
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- if (opt.remArgs.length<2) opt.usage_error();
- // Start SNMP API
- SnmpAPI api;
- api = new SnmpAPI();
- if (values[0].equals("Set")) api.setDebug( true );
- // Open session
- SnmpSession session = new SnmpSession(api);
- session.setProtocol(session.TRANSPORT_PROVIDER);
- SetValues setVal = new SetValues( session, values );
- if(setVal.usage_error) opt.usage_error();
- ProtocolOptions params = null;
- if(values[3] != null) {
- params = new TcpProtocolOptionsImpl(opt.remArgs[0], Integer.parseInt( values[3] ), -1);
- }
- else {
- params = new TcpProtocolOptionsImpl(opt.remArgs[0], 161, -1);
- }
- session.setProtocolOptions(params);
- // Build Get request PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand( api.GET_REQ_MSG );
- // add OIDs
- for (int i=1;i<opt.remArgs.length;i++) {
- SnmpOID oid = new SnmpOID(opt.remArgs[i]);
- if (oid.toValue() == null)
- System.err.println("Invalid OID argument: " + opt.remArgs[i]);
- else pdu.addNull(oid);
- }
- try {
- //Open session
- session.open();
- } catch (SnmpException e) {
- System.err.println("Error opening session:"+e.getMessage());
- System.exit(1);
- }
- if(session.getVersion()==SnmpAPI.SNMP_VERSION_3) {
- pdu.setUserName(setVal.userName.getBytes());
- USMUtils.init_v3_params(setVal.userName, setVal.authProtocol, setVal.authPassword, setVal.privPassword, params.getSessionId(),session);
- pdu.setContextName(setVal.contextName.getBytes());
- pdu.setContextID(setVal.contextID.getBytes());
- }
- try {
- // Send PDU and receive response PDU
- pdu = session.syncSend(pdu);
- } catch (SnmpException e) {
- System.err.println("Sending PDU"+e.getMessage());
- session.close();
- api.close();
- System.exit(1);
- }
- if (pdu == null) {
- // timeout
- System.out.println("Request timed out to: " + opt.remArgs[0] );
- session.close();
- api.close();
- System.exit(1);
- }
- // print and exit
- System.out.println("Response PDU received from " +((TcpProtocolOptionsImpl)(pdu.getProtocolOptions())).getRemoteHost()+
- ", community: " + pdu.getCommunity());
- // Check for error in response
- if (pdu.getErrstat() != 0)
- System.err.println(pdu.getError());
- else
- // print the response pdu varbinds
- System.out.println(pdu.printVarBinds());
- // close session
- session.close();
- // stop api thread
- api.close();
- System.exit(0);
- }
- }