我有以下问题。我正在从SonicWall中删除一些防火墙规则。我这样做的方法是让声音墙中的所有规则找到匹配的规则,将它们存储在列表中并使用此列表发出命令以删除它们。删除规则的方法是使用SSH和带有ID的命令。我遇到的问题是,每次删除ID时,所有ID都会被更改,导致第二个命令尝试删除不存在的访问规则(或者更糟的是错误的访问规则)。我为了克服这一点,在每次删除过程后我刷新列表,我命令获得更新的元素,从而获得新的ID。我面临的问题是列表的更新不正确(它发生在每个循环中)因为列表被用作迭代器(导致发出错误的命令)。
使用新元素更新此列表的最佳和最安全的方法是什么?
以下是相关代码的一部分:
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
accessRules = getAccessRules(multiResult, accessRule);
if(accessRules == null){
Trc.trc("No access rules found to delete");
return false;
}
numberOfRulesToDelete = accessRules.size();
int size = numberOfRulesToDelete;
IntList deleteRulesId = new IntList();
while(numberOfRulesToDelete != 0){
for(AccessRule arToDelete : accessRules){
expect.sendLine("configure");
multiResult = expect.expect(anyOf(regexp("(.*)" + confmodePromptPattern)));
Result showCmdResult = multiResult.getResults().get(0);
GenericCmdDto outPut = null;
ObjectMapper objectMapper = new ObjectMapper();
GenericFailedCmdDto failedOutPut = null;
String swResponse = null;
swResponse = handleSWConfModeResponse(showCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){//if change to configure command was successfull
Result confCmdResult = null;
Trc.trc("Configure command: " + outPut.getSuccess());
expect.sendLine("no access-rule id " + arToDelete.getIndex());
MultiResult confResult = expect.expect(anyOf(regexp("(.*)" + confmodePromptPattern)));
confCmdResult = confResult.getResults().get(0);
swResponse = handleSWConfModeResponse(confCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){//if the delete command was successfull
Trc.trc("Delete command issued successfully");
expect.sendLine("commit");
confCmdResult = expect.expect(anyOf(regexp("(.*)" + confmodePromptPattern)));
swResponse = handleSWConfModeResponse(confCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){//if the commit command was successfull
Trc.trc("Commit command issued succesfully!");
Trc.trc("Deleted successfully access rule: " + arToDelete.getIndex());
deleteRulesId.addElement(arToDelete.getIndex());
expect.sendLine("exit");//change to no conf mode
confCmdResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
swResponse = handleSWPromptModeResponse(confCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){//if the exit command was successfull
Trc.trc("Exit command issued successfully!");
operationSuccess = outPut.getSuccess();
}else{//if exit unsucessfull
failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
operationSuccess = outPut.getSuccess();
Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
Trc.trc("Changing to show mode....");
expect.sendLine("exit");
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
showCmdResult = multiResult.getResults().get(0);
swResponse = handleSWPromptModeResponse(confCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){
Trc.trc("Exiting from conf mode..");
Trc.trc("Exit command: " + outPut.getSuccess());
}
return operationSuccess;
}
}else{//if commit unsucessfull
failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
operationSuccess = outPut.getSuccess();
Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
Trc.trc("Changing to show mode....");
expect.sendLine("exit");
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
showCmdResult = multiResult.getResults().get(0);
swResponse = handleSWPromptModeResponse(showCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){
Trc.trc("Exiting from conf mode..");
Trc.trc("Exit command: " + outPut.getSuccess());
}
return operationSuccess;
}
}else{//if delete unsucessfull
failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
operationSuccess = outPut.getSuccess();
Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
Trc.trc("Changing to show mode....");
expect.sendLine("exit");
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
showCmdResult = multiResult.getResults().get(0);
swResponse = handleSWPromptModeResponse(showCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){
Trc.trc("Exiting from conf mode..");
Trc.trc("Exit command: " + outPut.getSuccess());
}
return operationSuccess;
}
}
else{//if configure unsuccessfull
failedOutPut = objectMapper.readValue(createFailedJSONResponse(swResponse.getBytes()), GenericFailedCmdDto.class);
operationSuccess = outPut.getSuccess();
Trc.trc("Commit command has failed! Message: " + failedOutPut.getMessage() + ", Code: " + failedOutPut.getCode());
Trc.trc("Changing to show mode....");
expect.sendLine("exit");
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
showCmdResult = multiResult.getResults().get(0);
swResponse = handleSWPromptModeResponse(showCmdResult);
outPut = objectMapper.readValue(createSuccessfullJSONResponse(swResponse.getBytes()), GenericCmdDto.class);
if(outPut.getSuccess()){
Trc.trc("Exiting from conf mode..");
Trc.trc("Exit command: " + outPut.getSuccess());
}
return operationSuccess;
}
expect.sendLine("show access-rules from " + accessRule.getFromZone() + " to " + accessRule.getToZone());
multiResult = expect.expect(anyOf(regexp("(.*)" + promptPattern)));
accessRules = getAccessRules(multiResult, accessRule);
if(accessRules != null){
numberOfRulesToDelete = accessRules.size();
}else{
Trc.trc("Deleting process complete!");
numberOfRulesToDelete = 0;
for(int i = 0; i < deleteRulesId.size(); i++){
Trc.trc("Deleted " + size + " successfully rules: " + deleteRulesId.getElementAt(i));
}
}
}
}
return operationSuccess;
答案 0 :(得分:1)
在迭代时不应更新列表。
改为使用副本。
答案 1 :(得分:0)
是否可以帮助您使用列表的迭代器。您可以使用迭代器删除方法并立即从列表中删除该项目。