📄 snmpusmremoteconfigure.java
字号:
/* $Id: snmpUSMRemoteConfigure.src,v 1.4.2.2 2004/03/17 10:05:30 vivek Exp $ *//* * @(#)snmpUSMRemoteConfigure.java * Copyright (c) 1996-2004 AdventNet, Inc. All Rights Reserved. * Please read the associated COPYRIGHTS file for more details. *//** * This is an example program to explain how remotely configure users * on the agent. The procedure followed is a five step procedure. * Step 1. GET(usmUserSpinLock.0) Value * Step 2. SET(usmUserSpinLock.0=spinlockValue * usmUserCloneFrom=templateUser, * usmUserStatus=createAndWait) * Strp 3. GET(usmUserSpinLock.0) value * SET(usmUserSpinLock.0=spinLockValue, * usmUserKeyChange=keyChangeValue * usmUserPublic=randomValue) * Strp 4. GET(usmUserPulic) and check it has randomValue * Step 5. Activate the new user by setting the usmUserStatus=active * * The application sends request with version v3. * The user could run this application by giving any of the following usage. * * java snmpUSMRemoteConfigure [options] userName newUserName hostname * * iava snmpUSMRemoteConfigure [-d] [-p port] [-r retries] [-t timeout] [-a auth_protocol] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID] [-y new_auth_password] [-z new_priv_password] userName newUserName host * * e.g. * java snmpUSMRemoteConfigure -a MD5 -w initial2Pass -y initial2NewPass initial2 newInitial 10.3.2.120 * Where, initial2 user already configured on the agent whose authProtocol is * MD5 and authPassword is initial2Pass. newInitial is the name of the new * user who will be configured with authProtocol=MD5 and * authPassword=initial2NewPass. * Here the clone-from user is initial2, the user on whose behalf all the * requests will be sent. * * Options: * [-d] - Debug output. By default off. * [-p] <port> - remote port no. By default 161. * [-t] <Timeout> - Timeout. By default 5000ms. * [-r] <Retries> - Retries. By default 0. * [-a] <autProtocol> - The authProtocol(MD5/SHA) of the template user. Mandatory if authPassword is specified * [-w] <authPassword> - The authentication password of the template user. * [-s] <privPassword> - The privacy protocol password of the template user. 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. * [-w] <newAuthPassword> - The authentication password for the new user. * [-s] <newPrivPassword> - The privacy protocol password of the new user. Must be accompanied with auth password and authProtocol fields. * username Mandatory - The user who is already configured on the agent. * (template user) * newusername Mandatory - The user name of the new user who will be Remotely configured on the agent. * host Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress). */import java.lang.*;import java.util.*;import java.net.*;import com.adventnet.snmp.snmp2.*;import com.adventnet.snmp.snmp2.usm.*;import com.adventnet.utils.*;public class snmpUSMRemoteConfigure { private static final int USM_SECURITY_MODEL = 3; private static final String ENC = "ISO8859_1"; private static final int DEBUG = 0; private static final int PORT = 1; private static final int RETRIES = 2; private static final int TIMEOUT = 3; private static final int AUTH_PROTOCOL = 4; private static final int AUTH_PASSWORD = 5; private static final int PRIV_PASSWORD = 6; private static final int CONTEXT_NAME = 7; private static final int CONTEXT_ID = 8; private static final int NEW_AUTH_PASSWORD = 9; private static final int NEW_PRIV_PASSWORD = 10; private static final String SPIN_LOCK_OID = ".1.3.6.1.6.3.15.1.2.1.0"; private static final String USM_TABLE = ".1.3.6.1.6.3.15.1.2.2"; private static final String USM_ENTRY = ".1.3.6.1.6.3.15.1.2.2.1"; private static final String CLONE_COL = "4"; private static final String KEY_CHANGE_COL = "6"; private static final String PRIV_KEY_CHANGE_COL = "9"; private static final String USM_PUBLIC_COL = "11"; private static final String ROW_STATUS_COL = "13"; private static final int AUTH_MD5_LEN = 16; private static final int AUTH_SHA_LEN = 20; boolean debug = false; public static void main(String args[]) { snmpUSMRemoteConfigure surg = new snmpUSMRemoteConfigure(); // Take care of getting options String usage = "snmpUSMRemoteConfigure [-d] [-p port] [-r retries] \n" + "[-t timeout] [-a auth_protocol] [-w auth_password] \n" + "[-s priv_password] [-n contextName] [-i contextID] \n" + "[-y new_auth_password] [-z new_priv_password] \n" + "userName newUserName host "; String options[] = { "-d", "-p", "-r", "-t", "-a", "-w", "-s", "-n", "-i", "-y", "-z" }; String values[] = { "None", null, null, null, null, null, null, null, null, null, null }; String userName = new String(""); int authProtocol = USMUserEntry.NO_AUTH; String authPassword = new String (""); String privPassword = new String (""); String contextName = new String (""); String contextID = new String (""); String newUser = new String(""); //int newAuthProtocol = USMUserEntry.NO_AUTH; String newAuthPassword = new String (""); String newPrivPassword = new String (""); ParseOptions opt = new ParseOptions(args,options,values, usage); if (opt.remArgs.length<3) opt.usage_error(); // Start SNMP API SnmpAPI api; api = new SnmpAPI(); if (values[DEBUG].equals("Set")){ api.setDebug( true ); surg.debug = true; } userName = opt.remArgs[0]; newUser = opt.remArgs[1]; // Open session SnmpSession session = new SnmpSession(api); // set remote Host UDPProtocolOptions ses_opt = new UDPProtocolOptions(opt.remArgs[2]); // Set the values accepted from the command line //boolean usage_error = false; //set remote Port, timeout,retries if needed. try { if (values[PORT] != null) ses_opt.setRemotePort( Integer.parseInt(values[PORT]) ); if (values[RETRIES] != null) session.setRetries( Integer.parseInt(values[RETRIES]) ); if (values[TIMEOUT] != null) session.setTimeout( Integer.parseInt(values[TIMEOUT]) ); } catch (NumberFormatException ex) { System.err.println("Invalid Integer Arg"); } session.setProtocolOptions(ses_opt); session.setVersion( SnmpAPI.SNMP_VERSION_3 ); if ((values[AUTH_PROTOCOL] != null) && (values[AUTH_PASSWORD] != null) && (values[NEW_AUTH_PASSWORD] != null)) { if(values[AUTH_PROTOCOL].equals("SHA")) authProtocol = USMUserEntry.SHA_AUTH; else authProtocol = USMUserEntry.MD5_AUTH; if(authProtocol==USMUserEntry.NO_AUTH){ System.err.println("Enter authentication protocol"); opt.usage_error(); } authPassword = values[AUTH_PASSWORD]; newAuthPassword = values[NEW_AUTH_PASSWORD]; if (values[PRIV_PASSWORD] != null) { if (values[NEW_PRIV_PASSWORD] != null) { privPassword = values[PRIV_PASSWORD]; newPrivPassword = values[NEW_PRIV_PASSWORD]; } else opt.usage_error(); } } else if ((values[AUTH_PROTOCOL] != null) || (values[AUTH_PASSWORD] != null) || (values[PRIV_PASSWORD] != null)) { opt.usage_error(); } if (values[CONTEXT_NAME] != null) contextName = values[CONTEXT_NAME]; if (values[CONTEXT_ID] != null) contextID = values[CONTEXT_ID]; // Build Get request PDU SnmpPDU pdu = new SnmpPDU(); try { //Open session session.open(); } catch (SnmpException e) { System.err.println("Error opening session:"+e.getMessage()); System.exit(1); } // inititialize the manager by adding the user. All requests will // sent with this username pdu.setUserName(userName.getBytes()); try { USMUtils.init_v3_parameters( userName, authProtocol, authPassword, privPassword, ses_opt.getRemoteHost(), ses_opt.getRemotePort(), session); } catch(Exception exp) { System.out.println(exp.getMessage()); System.exit(1); } pdu.setContextName(contextName.getBytes()); pdu.setContextID(contextID.getBytes()); // A valid user is now configured.on the manager. System.out.println("A new user " + userName + " is now " + "configured on the manager"); // Let's start the remote configuration. // Step 1. Retrive the USMUserSpinLock int spinLock = surg.sendSpinLockRequest(pdu,session); if(spinLock < 0){ System.out.println("Error in retriving SnmpLock"); System.exit(1); } System.out.println("Spin lock value retrived successfully\n"); // Since we are reusing the PDU, we will remove the varbinds // and set the reqid to 0. surg.removeAllVarBinds(pdu); //pdu.setReqid(0); // Step 2. Send a multiVarBind SET request with the retrived // spinlock value, USMUserCloneFrom=templateuser and // usmUserStatus=createAndWait. // Here the templateuser will be user on whose behalf the requests // are made. // Create the ConeFrom OID for the creating new instance. // i.e for newUserName and engineID byte[] engineID = ((Snmp3Message)pdu.getMsg()). getSecurity().getEngineID(); String engID; try{ engID = new String(engineID, ENC); } catch(Exception e){ engID = new String(engineID); } int[] firstindex = surg.stringToIntegerArray(engID); int[] secondIndex = surg.stringToIntegerArray(newUser); String engIDOID = surg.intArrayToString(firstindex); String newUserOID = surg.intArrayToString(secondIndex); String cloneFromOID = USM_ENTRY + "." + CLONE_COL + "." + firstindex.length + engIDOID + "." + secondIndex.length + newUserOID; if(surg.debug) System.out.println("cloneFrom OID = " + cloneFromOID); // Create the CloneFrom OID value. This will point // to the existing template entry.i.e userName and engineID int[] cloneFromIndex = surg.stringToIntegerArray(userName); String userNameOID = surg.intArrayToString(cloneFromIndex); String cloneFromOIDValue = USM_ENTRY + "." + CLONE_COL + "." + firstindex.length + engIDOID + "." + cloneFromIndex.length + userNameOID; if(surg.debug) System.out.println("cloneFrom OID value = " + cloneFromOIDValue); String rowStatusOID = USM_ENTRY + "." + ROW_STATUS_COL + "." + + firstindex.length + engIDOID + "." + secondIndex.length + newUserOID; if(surg.debug) System.out.println("Row status OID = " + rowStatusOID);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -