📄 snmpusmkeychange.java
字号:
System.out.println("Sending a request to set the privKeyChange\n"); pdu1.setCommand( SnmpAPI.SET_REQ_MSG ); try { // Send PDU and receive response PDU pdu1 = session.syncSend(pdu1); } catch (SnmpException e) { System.err.println("Sending PDU"+e.getMessage()); System.exit(1); } if (pdu1 == null) { // timeout System.out.println("Request timed out to: " + opt.remArgs[0] ); System.exit(1); } System.out.println("Response PDU for keyChange received from " + pdu1.getProtocolOptions().getSessionId()); if (pdu1.getErrstat() != 0) { System.out.println("KeyChange SET request returned error " + "User NOT Successfully cloned"); System.err.println(pdu1.getError()); System.exit(1); } else { // print the response pdu varbinds System.out.println(pdu1.printVarBinds()); } // Since we are reusing the PDU, we will remove the varbinds // and set the reqid to 0. surg.removeAllVarBinds(pdu1); //pdu.setReqid(0); if(ownKeyChange) { entry.setPrivPassword(newPrivPassword.getBytes()); byte[] engID1 = null; try { engID1 = engID.getBytes(ENC); } catch(Exception ex) { engID1 = engID.getBytes(); } // This is to set the new PrivKey on our side for receiving //the usmUserPublic // value after it has been set on the Agent. byte[] tempKey = USMUtils.password_to_key(authProtocol, newPrivPassword.getBytes(), newPrivPassword.getBytes().length, engID1); byte[] newPrivKey = new byte[16]; System.arraycopy(tempKey,0,newPrivKey,0,16); entry.setPrivKey(newPrivKey); } // Get the usmUserPublic value pdu1.setCommand( SnmpAPI.GET_REQ_MSG ); oid = new SnmpOID(randomOID); if (oid.toValue() == null) { System.err.println("Invalid OID argument: " + randomOID); } else { pdu1.addNull(oid); } try { // Send PDU and receive response PDU pdu1 = session.syncSend(pdu1); } catch (SnmpException e) { System.err.println("Sending PDU "+e.getMessage()); System.exit(1); } if (pdu1 == null) { // timeout System.out.println("Request timed out to: " + opt.remArgs[0] ); System.exit(1); } // print and exit System.out.println("Response PDU for usmUserPublic received from " + pdu1.getProtocolOptions().getSessionId()); // Check for error in response if (pdu1.getErrstat() != 0) { System.out.println("usmUserPublic GET request returned error " + "User NOT Successfully cloned"); System.err.println(pdu1.getError()); System.exit(1); } else { // print the response pdu varbinds System.out.println(pdu1.printVarBinds()); } userPublic = (pdu1.getVariable(0)).toString(); try { tempRandom = new String(priv_random,ENC); } catch(Exception e) { tempRandom = new String(priv_random); } if(userPublic.equals(tempRandom)) { System.out.println("usmUserPulic value is set appropriately\n"); } else { System.out.println("usmUserPulic value is NOT set appropriately"); System.out.println("User NOT Successfully cloned"); System.exit(1); } } System.out.println("Key Change SUCCESSFULL!!!"); // close session session.close(); // stop api thread api.close(); } int sendSpinLockRequest(SnmpPDU pdu,SnmpSession session) { // Get the usmUserSpinLock value. System.out.println("\nSending a request for retriving the " + " usmUserSpinLock value\n"); pdu.setCommand( SnmpAPI.GET_REQ_MSG ); SnmpOID oid = new SnmpOID(SPIN_LOCK_OID); if (oid.toValue() == null) { System.err.println("Invalid OID argument: " + SPIN_LOCK_OID); } else { pdu.addNull(oid); } try { // Send PDU and receive response PDU pdu = session.syncSend(pdu); } catch (SnmpException e) { System.err.println("Sending PDU "+e.getMessage()); return -1; } if (pdu == null) { // timeout System.out.println("Request timed out to: remote host "); return -1; } // print and exit System.out.println("Response PDU for usmUserSpinLock received from " + pdu.getProtocolOptions().getSessionId()); // Check for error in response if (pdu.getErrstat() != 0) { System.err.println(pdu.getError()); return -1; } else { // print the response pdu varbinds System.out.println(pdu.printVarBinds()); } SnmpVarBind vb = pdu.getVariableBinding(0); SnmpVar var = vb.getVariable(); int spinLock = Integer.parseInt(var.toString()); return spinLock; } String getKeyChangeValue(String engID, int protocol,String password, byte[] keyOld,byte[] random,boolean isPriv) { byte[] engID1 = null; try { engID1 = engID.getBytes(ENC); } catch(Exception exp) { engID1 = engID.getBytes(); } byte[] localizedKey = USMUtils.password_to_key( protocol, password.getBytes(), password.getBytes().length, engID1); int keyLength = AUTH_MD5_LEN; int hashLength = AUTH_MD5_LEN; if(protocol == USMUserEntry.SHA_AUTH && !isPriv) { keyLength = AUTH_SHA_LEN; hashLength = AUTH_SHA_LEN; } if(debug) { System.out.println("The old localized key is " + USMUtils.printOctets(keyOld, keyOld.length) + "\n"); System.out.println("The new localized key is " + USMUtils.printOctets(localizedKey, localizedKey.length) + "\n"); } byte[] keyChange = USMUtils.getKeyChange( protocol, true, keyLength, hashLength, localizedKey, keyOld, random); if(debug) { System.out.println("The keyChange is " + USMUtils.printOctets(keyChange, keyChange.length) + "\n"); System.out.println("The random is " + USMUtils.printOctets(random, random.length) + "\n"); } String kChange; try { kChange = new String(keyChange,ENC); } catch(Exception e) { kChange = new String(keyChange); } return kChange; } void removeAllVarBinds(SnmpPDU pdu) { int size = pdu.getVariableBindings().size(); for(int i = 0; i < size; i++) { pdu.removeVariableBinding(0); } } static public int[] stringToIntegerArray(String str) { int[] instanceOID = new int[str.length()]; byte[] temp = null; try { temp = str.getBytes(ENC); } catch(Exception ex) { temp = str.getBytes(); } for (int i=0;i<temp.length;i++) { instanceOID[i] = (int)(temp[i]) & 0xff; } return instanceOID; } static String intArrayToString(int[] intArray) { StringBuffer s = new StringBuffer(); for (int i=0;i<intArray.length;i++) { s.append("."+Integer.toString(intArray[i])); } return s.toString(); } /** adds the varbind with specified oid, type and value to the pdu */ static void addvarbind(SnmpPDU pdu, SnmpOID oid, String type, String value) { byte dataType ; if (type.equals("INTEGER")) { dataType = SnmpAPI.INTEGER; } else if (type.equals("STRING")) { dataType = SnmpAPI.STRING; } else if (type.equals("GAUGE")) { dataType = SnmpAPI.GAUGE; } else if (type.equals("TIMETICKS")) { dataType = SnmpAPI.TIMETICKS; } else if (type.equals("OPAQUE")) { dataType = SnmpAPI.OPAQUE; } else if (type.equals("IPADDRESS")) { dataType = SnmpAPI.IPADDRESS; } else if (type.equals("COUNTER")) { dataType = SnmpAPI.COUNTER; } else if (type.equals("OID")) { dataType = SnmpAPI.OBJID; } else if (type.equals("BITS")) { dataType = SnmpAPI.STRING; } else { System.err.println("Invalid variable type: " + type); return; } SnmpVar var = null; try { // create SnmpVar instance for the value and the type var = SnmpVar.createVariable( value, dataType ); } catch(SnmpException e) { System.err.println("Cannot create variable: " + oid + " with value: " + value); return; } //create varbind SnmpVarBind varbind = new SnmpVarBind(oid, var); // add variable binding pdu.addVariableBinding(varbind); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -