📄 snmpvacmconfigure.java
字号:
int match = Integer.parseInt(st.nextToken()); String readView = st.nextToken(); String writeView = st.nextToken(); String notifyView = st.nextToken(); // int storageType = Integer.parseInt(st.nextToken()); String index = getStringIndex(groupName) + "." + getStringIndex(prefix) + "." + model + "." + level; SnmpPDU pdu = getSnmpPDU(); SnmpVarBind varbind = new SnmpVarBind( new SnmpOID(vacmAccessEntry + "9." + index), new SnmpInt(4)); pdu.addVariableBinding(varbind); varbind = new SnmpVarBind( new SnmpOID(vacmAccessEntry + "4." + index), new SnmpInt(match)); pdu.addVariableBinding(varbind); varbind = new SnmpVarBind( new SnmpOID(vacmAccessEntry + "5." + index), new SnmpString(readView)); pdu.addVariableBinding(varbind); varbind = new SnmpVarBind( new SnmpOID(vacmAccessEntry + "6." + index), new SnmpString(writeView)); pdu.addVariableBinding(varbind); varbind = new SnmpVarBind( new SnmpOID(vacmAccessEntry + "7." + index), new SnmpString(notifyView)); pdu.addVariableBinding(varbind);/* varbind = new SnmpVarBind( new SnmpOID(vacmAccessEntry + "8." + index), new SnmpInt(storageType)); pdu.addVariableBinding(varbind);*/ send(pdu); System.out.println( "Successfully added an entry to the VacmAccessTable."); } catch(Exception exp) { System.out.println( "Unable to create an entry in VacmAccessTable. " + exp); } } private void deleteAccess() { try { if(params == null) { System.out.println( "Please enter the following in a single line " + "seperated by space:\n" + "GROUPNAME CONTEXTPREFIX SECURITYMODEL SECURITYLEVEL"); } String s = (params == null)? readLine() : params; StringTokenizer st = new StringTokenizer(s, " "); String groupName = st.nextToken(); String contextPrefix = st.nextToken(); int securityModel = Integer.parseInt(st.nextToken()); int securityLevel = Integer.parseInt(st.nextToken()); String index = vacmAccessEntry + "9." + getStringIndex(groupName) + "." + getStringIndex(contextPrefix) + "." + securityModel + "." + securityLevel; SnmpOID indexOID = new SnmpOID(index); SnmpInt indexValue = new SnmpInt(6); SnmpPDU pdu = getSnmpPDU(); SnmpVarBind varbind = new SnmpVarBind(indexOID, indexValue); pdu.addVariableBinding(varbind); send(pdu); System.out.println("Successfully deleted the entry " + "from the VacmAccessTable."); } catch(Exception exp) { System.out.println("Problem encountered while deleting access: " + exp.toString()); } } private void createSecurityToGroup() { if(params == null) { System.out.println( "Please enter the following in a single line " + "seperated by space:\n" + "MODEL SECURITYNAME GROUPNAME"); // + "STORAGE_TYPE"); } String s = (params == null)? readLine() : params; StringTokenizer st = new StringTokenizer(s, " "); try { int model = Integer.parseInt(st.nextToken()); String securityName = st.nextToken(); String groupName = st.nextToken(); // int storageType = Integer.parseInt(st.nextToken()); String index = model + "." + getStringIndex(securityName); SnmpPDU pdu = getSnmpPDU(); String rowStatusOID = securityToGroupTableEntry + "5." + index; SnmpOID oid = new SnmpOID(rowStatusOID); SnmpInt rowStatusValue = new SnmpInt(4); SnmpVarBind varbind = new SnmpVarBind(oid, rowStatusValue); pdu.addVariableBinding(varbind); String groupNameOID = securityToGroupTableEntry + "3." + index; oid = new SnmpOID(groupNameOID); SnmpString str = new SnmpString(groupName); varbind = new SnmpVarBind(oid, str); pdu.addVariableBinding(varbind);/* String storageTypeOID = securityToGroupTableEntry + "4." + index; oid = new SnmpOID(groupNameOID); SnmpInt storageTypeValue = new SnmpInt(storageType); varbind = new SnmpVarBind(oid, storageTypeValue); pdu.addVariableBinding(varbind);*/ send(pdu); System.out.println("Successfully added an entry " + "to the SecurityToGroupTable."); } catch(Exception exp) { System.out.println( "Unable to create an entry in the securityToGroupTable. " + exp); } } private void deleteSecurityToGroup() { if(params == null) { System.out.println( "Please enter the following in a single line " + "seperated by space:\n" + "MODEL SECURITYNAME"); } String s = (params == null)? readLine() : params; StringTokenizer st = new StringTokenizer(s, " "); try { int model = Integer.parseInt(st.nextToken()); String securityName = st.nextToken(); String index = model + "." + getStringIndex(securityName); String rowStatusOID = securityToGroupTableEntry + "5." + index; SnmpPDU pdu = getSnmpPDU(); SnmpOID rowStatus = new SnmpOID(rowStatusOID); SnmpInt rowStatusValue = new SnmpInt(6); SnmpVarBind varbind = new SnmpVarBind(rowStatus, rowStatusValue); pdu.addVariableBinding(varbind); send(pdu); System.out.println("Successfully deleted an entry from the " + "SecurityToGroypTable."); } catch(Exception exp) { System.out.println( "Unable to delete an entry from securityToGroupTable. " + exp.toString()); } } private void createView() { if(params == null) { System.out.println( "Please enter the following in a single line " + "seperated by space:\n" + "NAME SUBTREE MASK"); // + "STORAGE_TYPE"); } try { String s = (params == null) ? readLine() : params; StringTokenizer st = new StringTokenizer(s, " "); String viewName = st.nextToken(); String subtree = st.nextToken(); SnmpOID subtreeOID = new SnmpOID(subtree); int[] subids = (int[])subtreeOID.toValue(); if(subids != null) { subtree = subids.length + subtreeOID.toString(); } else { throw new Exception("Invalid SUBTREE: " + subtree); } String mask = st.nextToken(); // int storageType = Integer.parseInt(st.nextToken()); SnmpPDU pdu = getSnmpPDU(); String index = getStringIndex(viewName) + "." + subtree; SnmpOID rowStatusOID = new SnmpOID(vacmFamilyEntry+"6."+index); SnmpInt rowStatus = new SnmpInt(4); SnmpVarBind varbind = new SnmpVarBind(rowStatusOID, rowStatus); pdu.addVariableBinding(varbind); SnmpOID maskOID = new SnmpOID(vacmFamilyEntry + "3." + index); SnmpString maskValue = new SnmpString(mask); varbind = new SnmpVarBind(maskOID, maskValue); pdu.addVariableBinding(varbind); SnmpOID familyTypeOID = new SnmpOID(vacmFamilyEntry+"4."+index); SnmpInt familyTypeValue = new SnmpInt(1); varbind = new SnmpVarBind(familyTypeOID, familyTypeValue); pdu.addVariableBinding(varbind);/* SnmpOID storageTypeOID = new SnmpOID(vacmFamilyEntry+"5."+index); SnmpInt storageTypeValue = new SnmpInt(storageType); varbind = new SnmpVarBind(storageTypeOID, storageTypeValue); pdu.addVariableBinding(varbind);*/ send(pdu); System.out.println("Successfully added an entry to the " + "VacmViewTreeFamilyTable."); } catch(Exception exp) { System.out.println( "Unable to create an entry in VacmViewTreeFamilyTable. " + exp.toString()); } } private void deleteView() { if(params == null) { System.out.println( "Please enter the following in a single line " + "seperated by space:\n" + "NAME SUBTREE"); } try { String s = (params == null) ? readLine() : params ; StringTokenizer st = new StringTokenizer(s, " "); String viewName = st.nextToken(); String subtree = st.nextToken(); SnmpOID subtreeOID = new SnmpOID(subtree); int[] subids = (int[])subtreeOID.toValue(); if(subids != null) { subtree = subids.length + subtreeOID.toString(); } else { System.out.println("Invalid SUBTREE: " + subtree); } String index = getStringIndex(viewName) + "." + subtree ; SnmpPDU pdu = getSnmpPDU(); SnmpOID rowStatusOID = new SnmpOID(vacmFamilyEntry+"6."+index); SnmpInt rowStatusValue = new SnmpInt(6); SnmpVarBind varbind = new SnmpVarBind(rowStatusOID,rowStatusValue); pdu.addVariableBinding(varbind); send(pdu); System.out.println("Successfully deleted an entry from the " + "VacmViewTreeFamilyTable."); } catch(Exception exp) { System.out.println( "Unable to delete an entry from the VacmViewTreeFamilyTable. " + exp.toString()); } } private void help() { int num = 0; try { if(!helpNumber.equals("")) { num = Integer.parseInt(helpNumber); } } catch(Exception exp) { } if(num == 0 || num == CREATEACCESS || num == DELETEACCESS) { System.out.print(accessHelp); } if(num == 0 || num == CREATESEC2GROUP || num == DELETESEC2GROUP) { System.out.print(securityToGroupHelp); } if(num == 0 || num == CREATEVIEW || num == DELETEVIEW) { System.out.print(familyTableHelp); } } private void send(SnmpPDU pdu) throws Exception { SnmpPDU res_pdu = session.syncSend(pdu); if(res_pdu != null) { if(res_pdu.getErrindex() != 0 || res_pdu.getErrstat() != 0) { throw new Exception(res_pdu.getError()); } } else { throw new Exception("SNMP SET Request has timed out."); } } private String getStringIndex(String stringIndexValue) { int len = stringIndexValue.length(); StringBuffer sb = new StringBuffer(); sb.append(len); for(int i=0;i<len;i++) { sb.append("."); sb.append( (int)(stringIndexValue.charAt(i) & 0xff) ); } return sb.toString(); } private boolean isValidOption(int option) { return (option == CREATEACCESS || option == DELETEACCESS || option == CREATESEC2GROUP || option == DELETESEC2GROUP || option == DELETESEC2GROUP || option == CREATEVIEW || option == DELETEVIEW || option == HELP || option == QUIT); } private String readLine() { char newLine = '\n'; char ch; char[] array = new char[10]; int i=0; String line = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -