/* ---------------------------------------------------------------------------- Copyright (c) 2003 jSNMP Enterprises, All Rights Reserved Worldwide ------------------------------------------------------------------------------ Disclaimer: This software is provided AS IS, and without any warranty other than those which may be provided in a separate writing specifically referencing the software contained herein. All other warranties, except those which may be provided separately in writing, are expressly disclaimed. WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THERE IS NO WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE which is given with respect to this software or the operability thereof. ------------------------------------------------------------------------ */ // ------------------------------ Package ------------------------------------- // ------------------------------ Imports ------------------------------------- import java.net.*; import java.security.*; import java.io.*; import java.math.BigInteger; import com.outbackinc.services.protocol.snmp.*; import com.outbackinc.services.protocol.snmp.mib.*; import com.outbackinc.services.protocol.snmp.USM.*; /** * Sample which illustrates how to use the SnmpService to place * get-next orders. In this case, the walker walks the remote * agent's MIB until the end is reached. */ public class SnmpV3WalkerTest implements SnmpCustomer { // ------------------------------ Constants --------------------------------- // ------------------------------ Class variables --------------------------- // ------------------------------ Member (instance) variables --------------- private SnmpService m_service; private SnmpServiceConfiguration m_serviceConfiguration; private String[] m_szLastOID; private SnmpAuthoritativeSession m_cSnmpRemoteAuthoritativeSession; private SnmpOrderInfo m_orderinfo; private int m_iOrderNumStart; private SnmpMIBDictionary[] m_cSnmpMIBDictionaryList; private SnmpMIBService m_snmpMibService; // ------------------------------ Methods ----------------------------------- /** * Constructor */ public SnmpV3WalkerTest(String szHost, String szUserName, String szAuthPassword, String szPrivPassword, int iAuthScheme, int iPrivScheme, int iSecurityLevel, String szContextEngineID, String szContextName) { //Sets the initial OID to something which should be before the table m_szLastOID = new String[1]; m_szLastOID[0] = "1.3.6"; // load a mib dictionary so we can request by name m_snmpMibService = SnmpLocalInterfaces.getMIBService(); try { InputStream istream = jMIBC.loadMib("HOST-RESOURCES-MIB.my"); m_snmpMibService.loadMIB("HOST-RESOURCES-MIB", istream); } catch( Exception e ) { System.out.println("Couldn't load the HOST-RESOURCES-MIB MIB into the SnmpMIBService ... " + e.getMessage() + ".\n"); } // get of all the MIBs in the service for later use String[] szMibs = m_snmpMibService.listAvailableMIBs(); m_cSnmpMIBDictionaryList = new SnmpMIBDictionary[szMibs.length]; for (int i=0; i < szMibs.length; i++) { m_cSnmpMIBDictionaryList[i] = m_snmpMibService.getMIBDictionary(szMibs[i]); } //Initializes the Snmp Service try { m_service = SnmpLocalInterfaces.getService(); } catch (SocketException se) { System.out.println("Unable to initialize the Snmp service (" + se.getMessage() + ")."); return; } //This sample app runs synchronously--each request completes before the next //starts--so there is no possibility of packing PDU's and therefore no benefit //for having BufferDelay > 0. We change it through the SnmpServiceConfiguration API. m_serviceConfiguration = SnmpLocalInterfaces.getServiceConfiguration(m_service); m_serviceConfiguration.setBufferDelay(0); m_orderinfo = new SnmpOrderInfo(2, 3, 0); USMSecurityInfo secInfo; try { try { secInfo = new USMSecurityInfo(szUserName.getBytes("ASCII"), szAuthPassword.getBytes("ASCII"), szPrivPassword.getBytes("ASCII"), iAuthScheme, iPrivScheme, (byte)iSecurityLevel); } catch(UnsupportedEncodingException uee) { secInfo = new USMSecurityInfo(szUserName.getBytes(), szAuthPassword.getBytes(), szPrivPassword.getBytes(), iAuthScheme, iPrivScheme, (byte)iSecurityLevel); } } catch (SnmpSecurityException sse) { System.out.println("Unable to create secure session with remote agent " + szHost + " (" + sse.getMessage() + ")."); m_service.stop(); return; } SnmpAuthoritativeSessionFactory cSnmpAuthoritativeSessionFactory = SnmpLocalInterfaces.getAuthoritativeSessionFactory(); try { try { m_cSnmpRemoteAuthoritativeSession = cSnmpAuthoritativeSessionFactory.createRemoteAuthoritativeSession(szHost, 161, szContextEngineID.getBytes("ASCII"), szContextName.getBytes("ASCII"), secInfo); } catch(UnsupportedEncodingException uee) { m_cSnmpRemoteAuthoritativeSession = cSnmpAuthoritativeSessionFactory.createRemoteAuthoritativeSession(szHost, 161, szContextEngineID.getBytes(), szContextName.getBytes(), secInfo); } } catch (UnknownHostException uhe) { System.out.println("Unable to create secure session with remote agent as host (" + szHost + ") is unknown."); m_service.stop(); return; } catch (SnmpSecurityException sse) { System.out.println("Unable to create secure session with remote agent (" + szHost + ") due to discovery failure (" + sse.getMessage() + ")."); m_service.stop(); return; } m_iOrderNumStart = 1; System.out.println("Walking MIB Tree of " + szHost + " using SNMPv3"); System.out.println("\tStarting OID: " + m_szLastOID[0]); System.out.println("\tUser: " + szUserName); if (iSecurityLevel == SnmpSecurityLevels.noAuthNoPriv) { System.out.println("\tSecurity Level: noAuthNoPriv"); } else if (iSecurityLevel == SnmpSecurityLevels.AuthNoPriv) { System.out.println("\tSecurity Level: AuthNoPriv"); if (iAuthScheme == USMSecurityInfo.Auth_MD5) { System.out.println("\tAuthentication Scheme: MD5"); } else if (iAuthScheme == USMSecurityInfo.Auth_SHA) { System.out.println("\tAuthentication Scheme: SHA"); } else { System.out.println("\tAuthentication Scheme: unknown"); } System.out.println("\tAuthentication Password: " + szAuthPassword); } else if (iSecurityLevel == SnmpSecurityLevels.AuthPriv) { System.out.println("\tSecurity Level: AuthPriv"); if (iAuthScheme == USMSecurityInfo.Auth_MD5) { System.out.println("\tAuthentication Scheme: MD5"); } else if (iAuthScheme == USMSecurityInfo.Auth_SHA) { System.out.println("\tAuthentication Scheme: SHA"); } else { System.out.println("\tAuthentication Scheme: unknown"); } if (iPrivScheme == USMSecurityInfo.Priv_DES) { System.out.println("\tPrivacy Scheme: DES"); } else { System.out.println("\tPrivacy Scheme: unknown"); } System.out.println("\tAuthentication Password: " + szAuthPassword); System.out.println("\tPrivacy Password: " + szPrivPassword); } else { System.out.println("\tSecurity Level: unknown"); } System.out.println("\tContext Engine ID: " + szContextEngineID); System.out.println("\tContext Name: " + szContextName); // start the walker walking getNext(); } /** * Main method to start execution * * @param szArgs contains cmdline args, first of which must be the szHost, second must be read community * */ public static void main(String[] szArgs) { if (szArgs.length < 2) { usage(); return; } String szHost = szArgs[0]; try { try { // setup security provider Security.addProvider(new com.sun.crypto.provider.SunJCE()); } catch (Error err) { // do nothing ... encyption will fail if provider not installed } catch (Exception e) { // do nothing ... encyption will fail if provider not installed } String szUserName = szArgs[1]; String szAuthPassword = ""; String szPrivPassword = ""; String szContextEngineID = ""; String szContextName = ""; int iSecurityLevel = SnmpSecurityLevels.noAuthNoPriv; int iAuthScheme = USMSecurityInfo.Auth_MD5; int iPrivScheme = USMSecurityInfo.Priv_DES; for (int i = 2; i < szArgs.length; i += 2) { if (i == szArgs.length - 1) { usage(); return; } else if (szArgs[i].compareTo("-auth") == 0) { if (szArgs[i+1].compareTo("noAuthNoPriv") == 0) { iSecurityLevel = SnmpSecurityLevels.noAuthNoPriv; } else if (szArgs[i+1].compareTo("AuthNoPriv") == 0) { iSecurityLevel = SnmpSecurityLevels.AuthNoPriv; } else if (szArgs[i+1].compareTo("AuthPriv") == 0) { iSecurityLevel = SnmpSecurityLevels.AuthPriv; } else { usage(); return; } } else if (szArgs[i].compareTo("-authscheme") == 0) { if (szArgs[i+1].compareTo("MD5") == 0) { iAuthScheme = USMSecurityInfo.Auth_MD5; } else if (szArgs[i+1].compareTo("SHA") == 0) { iAuthScheme = USMSecurityInfo.Auth_SHA; } else { usage(); return; } } else if (szArgs[i].compareTo("-authpwd") == 0) { szAuthPassword = szArgs[i+1]; } else if (szArgs[i].compareTo("-privpwd") == 0) { szPrivPassword = szArgs[i+1]; } else if (szArgs[i].compareTo("-ctxengid") == 0) { szContextEngineID = szArgs[i+1]; } else if (szArgs[i].compareTo("-ctxname") == 0) { szContextName = szArgs[i+1]; } else { usage(); return; } } if (iSecurityLevel == SnmpSecurityLevels.AuthNoPriv) { if (szAuthPassword.compareTo("") == 0) { usage(); return; } } else if (iSecurityLevel == SnmpSecurityLevels.AuthPriv) { if (szAuthPassword.compareTo("") == 0 || szPrivPassword.compareTo("") == 0) { usage(); return; } } //Creates new walker SnmpV3WalkerTest test = new SnmpV3WalkerTest(szHost, szUserName, szAuthPassword, szPrivPassword, iAuthScheme, iPrivScheme, iSecurityLevel, szContextEngineID, szContextName); } catch (Exception e) { e.printStackTrace(); } } /** * * Displays correct usage */ public static void usage() { System.out.println("Usage : java SnmpV3WalkerTest hostname username [ -auth AuthNoPriv|AuthPriv -authscheme MD5|SHA -authpwd userspassword -privpwd userspassword -ctxengid contextengineid -ctxname contextname ]"); System.out.println("\thostname : Host or IP Address on which agent resides to walk (required as first argument)"); System.out.println("\tusername : user with permission to query agent (required as second argument)"); System.out.println("\t-auth noAuthNoPriv : do not use authentication or privacy to query agent (default)"); System.out.println("\t-auth AuthNoPriv : use authentication without privacy to query agent"); System.out.println("\t-auth AuthPriv : use authentication with privacy to query agent (requires -privpwd)"); System.out.println("\t-authscheme MD5 : use MD5 authentication scheme to query agent (default for -auth AuthNoPriv and -auth AuthPriv)"); System.out.println("\t-authscheme SHA : use SHA authentication scheme to query agent"); System.out.println("\t-authpwd userspassword : use \"userspassword\" as authentication password to query agent (required for -auth AuthPriv and -auth AuthNoPriv)"); System.out.println("\t-privpwd userspassword : use \"userspassword\" as privacy password to query agent (required for -auth AuthPriv)"); System.out.println("\t-ctxengid contextengineid : use \"contextengineid\" as context engine identifier to query agent"); System.out.println("\t-ctxname contextname : use \"contextname\" as context name to query agent"); } //SnmpCustomer Implementation /** * callback for delivering successes. In this case the implementation * is to print out the Object ID received and then use it for the basis * for the get-next request */ public void deliverSuccessfulOrder(int iOrderNum, SnmpVarBind vb) { String szOID = vb.getName(); boolean bFound = false; for (int i = 0; i < m_cSnmpMIBDictionaryList.length; i++) { String szName = m_cSnmpMIBDictionaryList[i].resolveOID(szOID); if (szName != null) { String szInfo = m_cSnmpMIBDictionaryList[i].resolveOIDType(szOID); if (szInfo != null) { if (szInfo.compareTo("regPt") == 0) { continue; } System.out.println(szOID + " as " + szName + " found in MIB " + m_cSnmpMIBDictionaryList[i].getMIBDictionaryName()); if (szInfo.compareTo("Enum") == 0) { int intValue = ((BigInteger)vb.getValue()).intValue(); String szEnumValue = m_cSnmpMIBDictionaryList[i].resolveEnum(szName, intValue); if (szEnumValue != null) System.out.println(szName + " (" + szOID + ") value = " + szEnumValue + " (" + intValue + ")"); else System.out.println(szName + " (" + szOID + ") value = " + vb.getStringValue() + " (couldn't resolve the string value of enum)"); } else { System.out.println(szName + " (" + szOID + ") value = " + vb.getStringValue()); } System.out.println(szName + " (" + szOID + ") type = " + szInfo); szInfo = m_cSnmpMIBDictionaryList[i].resolveOIDAbstractType(szOID); System.out.println(szName + " (" + szOID + ") abstract type = " + szInfo); } else { continue; } szInfo = m_cSnmpMIBDictionaryList[i].resolveOIDAccess(szOID); if (szInfo != null) System.out.println(szName + " (" + szOID + ") access = " + szInfo); szInfo = m_cSnmpMIBDictionaryList[i].resolveOIDStatus(szOID); if (szInfo != null) System.out.println(szName + " (" + szOID + ") status = " + szInfo); szInfo = m_cSnmpMIBDictionaryList[i].resolveOIDDescription(szOID); if (szInfo != null) System.out.println(szName + " (" + szOID + ") description = " + szInfo); bFound = true; break; } } if (!bFound) { System.out.println(szOID + " not found in any loaded MIB"); System.out.println(szOID + " value = " + vb.getStringValue()); } if (m_szLastOID[0].compareTo(szOID) != 0) { System.out.println(iOrderNum + ": " + szOID + " (" + vb.getStringValue() + ")"); m_szLastOID[0] = szOID; getNext(); } else { System.out.println("Finished"); m_service.stop(); } } /** * callback for failures. In this case if a failure is received, * stop walking the MIB */ public void deliverFailedOrder(int iOrderNum, int iErrorStatus) { System.out.println(iOrderNum + ": order failed (" + SnmpConstants.errorIDToString(iErrorStatus) + ")"); System.out.println("Ending MIB Walk"); m_service.stop(); } /** * gets the next object from the remote agent. Demonstrates * placing orders with the snmp service */ public void getNext() { m_iOrderNumStart = m_service.placeGetNextOrder(m_cSnmpRemoteAuthoritativeSession, m_orderinfo, false, m_szLastOID, this, m_iOrderNumStart); } }