📄 snmpusmkeychange.java
字号:
/* $Id: snmpUSMKeyChange.src,v 1.4.2.2 2004/03/17 10:05:30 vivek Exp $ *//* * @(#)snmpUSMKeyChange.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 * Strp 2. generate the keyChange value based on the secret * privKey of the clone-from user and the secret key * to be used for the new user. Let us call this keyChangeValue. * Strp 3. GET(usmUserSpinLock.0) value * SET(usmUserSpinLock.0=spinLockValue, * usmUserKeyChange=keyChangeValue * usmUserPublic=randomValue) * Strp 4. GET(usmUserPulic) and check it has randomValue * The application sends request with version v3. * The user could run this application by giving any of the following usage. * * java snmpUSMKeyChange [options] userName hostname * * iava snmpUSMKeyChange [-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 host * * e.g. * java snmpUSMKeyChange -a MD5 -w initial2Pass -y initial2NewPass initial2 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 snmpUSMKeyChange{ private static final int USM_SECURITY_MODEL = 3; private static final String ENC = "8859_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 int OLD_USERNAME = 11; private static final int OLD_AUTH_PASSWORD = 12; private static final int OLD_PRIV_PASSWORD = 13; 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 AUTH_OWN_KEY_CHANGE_COL = "7"; private static final String AUTH_KEY_CHANGE_COL = "6"; private static final String PRIV_OWN_KEY_CHANGE_COL = "10"; 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[]) { snmpUSMKeyChange surg = new snmpUSMKeyChange(); // Take care of getting options String usage = "snmpUSMKeyChange [-d] [-p port] [-r retries] [-t timeout] \n" + "[-a auth_protocol] [-w auth_password] [-s priv_password] \n" + "[-n contextName] [-i contextID] [-y new_auth_password] \n" + "[-z new_priv_password] [ -ou user_name] [ -ow old_auth_password] \n" + "[ -oz old_priv_password] userName host "; String options[] = { "-d", "-p", "-r", "-t", "-a", "-w", "-s", "-n", "-i", "-y", "-z", "-ou", "-ow", "-oz" }; String values[] = { "None", null, null, null, null, null, null, null, null, null, null, null, null,null }; String userName = new String(""); int authProtocol = USMUserEntry.NO_AUTH; String authPassword = null; String privPassword = null; String contextName = null; String contextID = null; //int newAuthProtocol = USMUserEntry.NO_AUTH; String newAuthPassword = null; String newPrivPassword = null; // Old sec Parameters for the user byte oldAuthProtocol = USMUserEntry.MD5_AUTH; String oldAuthPassword = null; String oldPrivPassword = null; String oldUserName = null; boolean ownKeyChange=false; 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 ); surg.debug = true; } userName = opt.remArgs[0]; // Open session SnmpSession session = new SnmpSession(api); // set remote Host UDPProtocolOptions ses_opt = new UDPProtocolOptions(); ses_opt.setRemoteHost(opt.remArgs[1]); // 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: " + ex.getMessage()); System.exit(1); } session.setProtocolOptions(ses_opt); session.setVersion( SnmpAPI.SNMP_VERSION_3 ); if ((values[AUTH_PROTOCOL] != null)) { if(values[AUTH_PROTOCOL].equals("SHA")) { authProtocol = USMUserEntry.SHA_AUTH; } else { authProtocol = USMUserEntry.MD5_AUTH; } } if(values[AUTH_PASSWORD]!=null) { authPassword=values[AUTH_PASSWORD]; } if(values[PRIV_PASSWORD]!=null) { privPassword=values[PRIV_PASSWORD]; } /// for old user if(values[OLD_USERNAME]==null) { ownKeyChange=true; } else { oldUserName=values[OLD_USERNAME]; } if(!ownKeyChange) { if(!(values[OLD_AUTH_PASSWORD]!=null && values[AUTH_PASSWORD]!=null && values[NEW_AUTH_PASSWORD]!=null)) { opt.usage_error(); } else { oldAuthPassword = values[OLD_AUTH_PASSWORD]; newAuthPassword = values[NEW_AUTH_PASSWORD]; authPassword = values[AUTH_PASSWORD]; } if(values[OLD_PRIV_PASSWORD]!=null) { if(values[PRIV_PASSWORD]==null) { opt.usage_error(); } else { if(values[NEW_PRIV_PASSWORD]==null) { opt.usage_error(); } else { oldPrivPassword = values[OLD_AUTH_PASSWORD]; newPrivPassword = values[NEW_AUTH_PASSWORD]; privPassword = values[AUTH_PASSWORD]; } } } else { privPassword=null; } } else { if(values[AUTH_PASSWORD]==null) { opt.usage_error(); } else { authPassword=values[AUTH_PASSWORD]; if(values[NEW_AUTH_PASSWORD]!=null) { newAuthPassword=values[NEW_AUTH_PASSWORD]; } else { opt.usage_error(); } } if(values[PRIV_PASSWORD]!=null) { privPassword=values[PRIV_PASSWORD]; if(values[NEW_PRIV_PASSWORD]!=null) { newPrivPassword=values[NEW_PRIV_PASSWORD]; } } } 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); } if(contextName!=null) { pdu.setContextName(contextName.getBytes()); } if(contextID!=null) { 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"); // Get the SpinLock to use in the next SET request. int spinLock = surg.sendSpinLockRequest(pdu,session); if(spinLock < 0) { System.out.println("Error in retriving SnmpLock"); System.exit(1); } // Since we are reusing the PDU, we will remove the varbinds // and set the reqid to 0. surg.removeAllVarBinds(pdu); //pdu.setReqid(0); 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); String engIDOID = surg.intArrayToString(firstindex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -