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

📄 snmpusmremoteconfigure.java

📁 this api for java to do the network managerment of system,i like it ,so share for a
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            // 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 (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);        // Step 4. GET usmUserPulic and check it has randomValue        // 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 " +                        pdu.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);        }        }    }  // end of if(authPassword.length() > 0 .....)            // Step 5. Activate the new user by setting the usmUserStatus=active         setOID3 = new SnmpOID(rowStatusOID);        surg.addvarbind(pdu, setOID3,"INTEGER","1");        System.out.println("Sending a request to set  rowStatus value to active\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[2] );            System.exit(1);        }        System.out.println("Response PDU for set row status active " +                " received from " + pdu.getProtocolOptions().getSessionId());        if (pdu.getErrstat() != 0){            System.out.println("Row Status 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());            System.out.println("User  S u c c e s s f u l l y  cloned !!!");        }        // close session        session.close();        // stop api thread        api.close();            System.exit(0);    }    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());            // System.exit(1);            return -1;        }            if (pdu == null) {            // timeout            System.out.println("Request timed out to: remote host ");            // System.exit(1);            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());            //System.exit(1);            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 user,String engID,                            int protocol,String password,                            byte[] keyOld,byte[] random,boolean isPriv)    {        byte[] engineID = null;        try {            engineID = engID.getBytes(ENC);        }catch(Exception ex)    {            engineID = engID.getBytes();        }        byte[] localizedKey = USMUtils.password_to_key(protocol,                                                password.getBytes(),                                                password.getBytes().length,                                                 engineID);        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 exp)        {            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();    } /** <img SRC="images/v3only.jpg" ALT="v3 only"> 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 + -