⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmpusmremoteconfigure.java

📁 this api for java to do the network managerment of system,i like it ,so share for a
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        // Create the multivarbind.         SnmpOID setOID1 = new SnmpOID(SPIN_LOCK_OID);        surg.addvarbind(pdu, setOID1,"INTEGER",                            new Integer(spinLock).toString());        SnmpOID setOID2 = new SnmpOID(cloneFromOID);         surg.addvarbind(pdu, setOID2,"OID",cloneFromOIDValue);        SnmpOID setOID3 = new SnmpOID(rowStatusOID);        surg.addvarbind(pdu, setOID3,"INTEGER","5");        System.out.println("\nSending a multivarbind cloneFrom request\n");        pdu.setCommand(SnmpAPI.SET_REQ_MSG);        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);        }        System.out.println("Response PDU for clonefrom  received from " +                            pdu.getProtocolOptions().getSessionId());        if (pdu.getErrstat() != 0){            System.out.println("Clonefrom request returned error "                                    + "User NOT Successfully cloned");            System.err.println(pdu.getError());            System.exit(1);        }        else             // print the response pdu varbinds            System.out.println(pdu.printVarBinds());        // Since we are reusing the PDU, we will remove the varbinds        // and set the reqid to 0.        surg.removeAllVarBinds(pdu);        //pdu.setReqid(0);        if(authPassword.length() > 0 && newAuthPassword.length() > 0)   {               // Get the SpinLock to use in the next SET request.        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);    // Step 3. Send a multiVarBind SET request with the retrived    // spinlock value, USMUserKeyChange=keyChangeValue and     // usmUserPublic=randomValue.        // Initialize the random value based on the protocol used.        byte[] random;        if(authProtocol == USMUserEntry.MD5_AUTH)            random = new byte[AUTH_MD5_LEN];        else            random = new byte[AUTH_SHA_LEN];        USMUserTable userTable = (USMUserTable)api.getSecurityProvider().                                    getTable(USM_SECURITY_MODEL);        USMUserEntry entry = null;        try{             entry = userTable.getEntry(userName.getBytes(),                                                engID.getBytes(ENC));        }catch(Exception e){             entry = userTable.getEntry(userName.getBytes(),                                                engID.getBytes());            }        byte[] oldKey = entry.getAuthKey();        // Generate the keyChange value based on the secret        // authKey of the clone-from user and the secret key        // to be used for the new user. Let us call this akcValue.        String akcValue = surg.getKeyChangeValue(newUser,engID,authProtocol,                                newAuthPassword,oldKey,random,false);        // This is just ot verify the keyChange. This will give        // the original key taking the keyChange. (This is what is         // done on the agent side.        if(authProtocol == USMUserEntry.MD5_AUTH){            try{            random = akcValue.substring(0,AUTH_MD5_LEN).getBytes(ENC);            }catch(Exception e){            random = akcValue.substring(0,AUTH_MD5_LEN).getBytes();            }        }        else{            try{            random = akcValue.substring(0,AUTH_SHA_LEN).getBytes(ENC);            }catch(Exception e){            random = akcValue.substring(0,AUTH_SHA_LEN).getBytes();            }        }        String keyChangeOID = USM_ENTRY + "." + KEY_CHANGE_COL + "."                 + firstindex.length + engIDOID + "."                 + secondIndex.length + newUserOID;        String randomOID = USM_ENTRY + "." + USM_PUBLIC_COL + "." +                + firstindex.length + engIDOID + "." +                 secondIndex.length + newUserOID;                        setOID1 = new SnmpOID(SPIN_LOCK_OID);        surg.addvarbind(pdu, setOID1,"INTEGER",                            new Integer(spinLock).toString());                setOID2 = new SnmpOID(keyChangeOID); //check this up        surg.addvarbind(pdu, setOID2,"STRING",akcValue);        setOID3 = new SnmpOID(randomOID);        String randomString;        try{            randomString = new String(random,ENC);        }catch(Exception e){            randomString = new String(random);        }        surg.addvarbind(pdu, setOID3,"STRING", randomString);        System.out.println("Sending a request to set the authKeyChange\n");        pdu.setCommand( SnmpAPI.SET_REQ_MSG );        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);        }        System.out.println("Response PDU for keyChange  received from " +                            pdu.getProtocolOptions().getSessionId());        if (pdu.getErrstat() != 0){            System.out.println("KeyChange SET request returned error "                                    + "User NOT Successfully cloned");            System.err.println(pdu.getError());            System.exit(1);        }        else             // print the response pdu varbinds            System.out.println(pdu.printVarBinds());        // Since we are reusing the PDU, we will remove the varbinds        // and set the reqid to 0.        surg.removeAllVarBinds(pdu);        //pdu.setReqid(0);    // Step 4. GET usmUserPulic and check it has randomValue        // Get the usmUserPublic value        pdu.setCommand( SnmpAPI.GET_REQ_MSG );                SnmpOID oid = new SnmpOID(randomOID);        if (oid.toValue() == null)             System.err.println("Invalid OID argument: " + randomOID);        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());            System.exit(1);        }            if (pdu == 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 " +                            pdu.getProtocolOptions().getSessionId());                    // Check for error in response        if (pdu.getErrstat() != 0){            System.out.println("usmUserPublic GET request returned error "                                    + "User NOT Successfully cloned");            System.err.println(pdu.getError());            System.exit(1);        }        else             // print the response pdu varbinds            System.out.println(pdu.printVarBinds());        String userPublic = (pdu.getVariable(0)).toString();        String tempRandom;        try{            tempRandom = new String(random,ENC);        }catch(Exception e){            tempRandom = new String(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);        }        // Since we are reusing the PDU, we will remove the varbinds        // and set the reqid to 0.        surg.removeAllVarBinds(pdu);        //pdu.setReqid(0);        // Start the privKeyChange        if(privPassword.length()>0 && newPrivPassword.length()>0)            {            newPrivPassword = values[NEW_PRIV_PASSWORD];            SnmpPDU pdu1 = new SnmpPDU();            pdu1.setUserName(userName.getBytes());            pdu1.setContextName(contextName.getBytes());            pdu1.setContextID(contextID.getBytes());            // Step 1. Retrive the USMUserSpinLock            spinLock = surg.sendSpinLockRequest(pdu1,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(pdu1);            // Step 3. Send a multiVarBind SET request with the retrived            // spinlock value, USMUserKeyChange=keyChangeValue and             // usmUserPublic=randomValue.            // Initialize the random value based on the protocol used.            byte[] priv_random = new byte[16]; // always 16 for Priv            byte[] oldPrivKey = entry.getPrivKey();                    // 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 pkcValue.            String pkcValue = surg.getKeyChangeValue(newUser,engID,USMUserEntry.MD5_AUTH,newPrivPassword,oldPrivKey,random,true);        // This is just to verify the keyChange. This will give        // the original key taking the keyChange. (This is what is         // done on the agent side.            try{            priv_random = pkcValue.substring(0,16).getBytes(ENC);            }catch(Exception e){            priv_random = pkcValue.substring(0,16).getBytes();            }            keyChangeOID = USM_ENTRY + "." + PRIV_KEY_CHANGE_COL + "."                 + firstindex.length + engIDOID + "."                 + secondIndex.length + newUserOID;            randomOID = USM_ENTRY + "." + USM_PUBLIC_COL + "." +                + firstindex.length + engIDOID + "." +                 secondIndex.length + newUserOID;                            setOID1 = new SnmpOID(SPIN_LOCK_OID);            surg.addvarbind(pdu1, setOID1,"INTEGER",                            new Integer(spinLock).toString());                    setOID2 = new SnmpOID(keyChangeOID); //check this up            surg.addvarbind(pdu1, setOID2,"STRING",pkcValue);            setOID3 = new SnmpOID(randomOID);            try{                randomString = new String(priv_random,ENC);            }catch(Exception e){            randomString = new String(priv_random);            }            surg.addvarbind(pdu1, setOID3,"STRING", randomString);            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) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -