/* $Id: snmpwalk.src,v 1.4 2002/09/09 05:43:43 pushpar Exp $ */ /* * @(#)snmpwalk.java * Copyright (c) 2012 ZOHO Corp. 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 WALK using com.adventnet.snmp.snmp2 and * com.adventnet.snmp.mibs packages of AdventNetSNMP2 api. * The user could run this application by giving any one of the following usage. * * java snmpwalk [options] hostname oid * * v1 request: * java snmpwalk [-d] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID * e.g. * java snmpwalk -p 161 -c public -m ../../../RFC1213-MIB adventnet .1.3.6.1 *v2c and v3 only * v2c request: * java snmpwalk [-d] [-v version(v1,v2)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID * e.g. For v1 request give -v v1 or drop the option -v . * java snmpwalk -p 161 -v v2 -c public -m ../../../RFC1213-MIB adventnet .iso.org *v3 only * v3 request: * java snmpwalk [-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 * e.g. * java snmpwalk -v v3 -u initial2 -w initial2Pass -a MD5 10.3.2.120 .1.3 * * 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 . * * If the mib is loaded you can also give string oids in the following formats * .iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0 or system.sysDescr.0 or * sysDescr.0 . * * Options: * [-d] - Debug output. By default off. * [-c] - community String. By default "public". * [-p] - remote port no. By default 161. * [-t] - Timeout. By default 5000ms. * [-r] - Retries. By default 0. * [-m] - MIB files.To load multiple mibs give within double quotes files seperated by a blank space. *v3 only [-v] - version(v1 / v2 / v3). By default v1. * [-u] - The v3 principal/userName * [-a] - The authProtocol(MD5/SHA). Mandatory if authPassword is specified * [-w] - The authentication password. * [-s] - The privacy protocol password. Must be accompanied with auth password and authProtocol fields. * [-n] - The contextName to be used for the v3 pdu. * [-i] - The contextID to be used for the v3 pdu. * Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress). * Mandatory - Give only one Object Identifier. */ import java.lang.*; import java.util.*; import java.net.*; import com.adventnet.snmp.snmp2.*; import com.adventnet.snmp.mibs.*; import com.adventnet.snmp.snmp2.usm.*; public class snmpwalk { private static final int DEBUG = 0; private static final int MIBS = 6; public static void main(String args[]) { // Take care of getting options String usage = "snmpwalk [-d] [-v version(v1,v2,v3)] [-m MIB_files] [-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 "; String options[] = { "-d", "-c", "-wc", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s", "-n", "-i"}; String values[] = { "None", null, null, null, null, null, null, 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[DEBUG].equals("Set")) api.setDebug( true ); // Open SnmpSession SnmpSession session = new SnmpSession(api); // set remote Host session.setPeername( opt.remArgs[0] ); //set the values SetValues setVal = new SetValues(session, values); if(setVal.usage_error) opt.usage_error(); // Loading MIBS . MibOperations mibOps = new MibOperations(); // To load MIBs from compiled file mibOps.setLoadFromCompiledMibs(true); if (values[MIBS] != null) try { System.err.println("Loading MIBs: "+values[MIBS]); mibOps.loadMibModules(values[MIBS]); } catch (Exception ex) { System.err.println("Error loading MIBs: "+ex); } // Build GetNext request PDU SnmpPDU pdu = new SnmpPDU(); pdu.setCommand( api.GETNEXT_REQ_MSG ); // need to save the root OID to walk sub-tree SnmpOID oid = mibOps.getSnmpOID(opt.remArgs[1]); if( oid == null) System.exit(1); int rootoid[] = (int[]) oid.toValue(); if (rootoid == null) { //if don't have a valid OID for first, exit System.err.println("Invalid OID argument: " + opt.remArgs[1]); System.exit(1); } else pdu.addNull(oid); try { // open sesssion session.open(); } catch (SnmpException e) { System.err.println("Error in open session : "+e.getMessage()); } if(session.getVersion()==SnmpAPI.SNMP_VERSION_3) { pdu.setUserName(setVal.userName.getBytes()); USMUtils.init_v3_params(setVal.userName, setVal.authProtocol, setVal.authPassword, setVal.privPassword, session.getPeername(),session.getRemotePort(),session); pdu.setContextName(setVal.contextName.getBytes()); pdu.setContextID(setVal.contextID.getBytes()); } // loop for each PDU in the walk while (true) { // until received OID isn't in sub-tree try { // Send PDU and receive response PDU pdu = session.syncSend(pdu); } catch (SnmpException e) { System.err.println("Sending PDU : " + e.getMessage()); System.exit(1); } if (pdu == null) { // timeout System.out.println("Request timed out to: " + opt.remArgs[0] ); System.exit(1); } // stop if outside of sub-tree if (!isInSubTree(rootoid,pdu)) break; // print response PDU if(pdu.getVersion() == SnmpAPI.SNMP_VERSION_1) { // check for error if (pdu.getErrstat() != 0) { System.out.println("Error Indication in response: " + SnmpException.exceptionString((byte)pdu.getErrstat()) + "\nErrindex: " + pdu.getErrindex()); System.exit(1); } // print response pdu variable-bindings System.out.println(mibOps.varBindsToString(pdu)); } else if((pdu.getVersion() == SnmpAPI.SNMP_VERSION_2C) || (pdu.getVersion() == SnmpAPI.SNMP_VERSION_3)) { for(Enumeration e = pdu.getVariableBindings().elements(); e.hasMoreElements() ;) { int error = 0; SnmpVarBind varbind = (SnmpVarBind)e.nextElement(); // check for error if ( (error = varbind.getErrindex()) != 0) { System.out.println("Error Indication in response: " + SnmpException.exceptionString((byte)error)); System.exit(1); } // print response pdu variable-bindings System.out.println(mibOps.varBindsToString(pdu)); } } else System.out.println("Invalid Version Number"); // set GETNEXT_REQ_MSG to do walk pdu.setCommand( api.GETNEXT_REQ_MSG ); // Don't forget to set request id to 0 otherwise next request will fail pdu.setReqid(0); } // end of while true // close session session.close(); // stop api thread api.close(); System.exit(0); } /** check if first varbind oid has rootoid as an ancestor in MIB tree */ static boolean isInSubTree(int[] rootoid, SnmpPDU pdu) { SnmpOID objID = (SnmpOID) pdu.getObjectID(0); if (objID == null) return false; int oid[] = (int[]) objID.toValue(); if (oid == null) return false; if (oid.length < rootoid.length) return false; for (int i=0;i