我在通过PHP API更新路由器上的规则时遇到了一些问题,如何让我的代码首先检查设置是否存在差异,例如首先检查路由器是否禁用规则/对象如果一切都完美匹配,则应用或不执行任何操作。
目前,每当我的同步脚本运行时,它都会在路由器上不断读取(更新)规则/对象,因为代码已经具有已在路由器上配置的相同信息。
当前代码:
function update_routerOS_address_list($list, $address, $comment) {
//Set globals
global $API;
global $Debug;
$API->write('/ip/firewall/address-list/print',false);
$API->write('?comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
if(count($ARRAY)>0){
$API->write('/ip/firewall/address-list/set',false);
$API->write("=.id=".$ARRAY[0]['.id'],false);
$API->write('=disabled=no',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
} else {
$API->write('/ip/firewall/address-list/add',false);
$API->write('=list='.$list,false);
$API->write('=address='.$address,false);
$API->write('=comment='.$comment,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
}
}
答案 0 :(得分:0)
我已经找到了想要的答案
function update_routerOS_address_list($list, $address, $comment) {
//Set globals
global $API;
global $Debug;
$API->write('/ip/firewall/address-list/print',false);
$API->write('?list='.$list,false);
$API->write('?address='.$address,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
//Get info from router and apply
if(count($ARRAY)>0){
$r_list = ($ARRAY['0']['list']);
$r_address = ($ARRAY['0']['address']);
$r_comment = ($ARRAY['0']['comment']);
$r_disabled = ($ARRAY['0']['disabled']);
} else {
$r_list = NULL;
$r_address = NULL;
$r_comment = NULL;
$r_disabled = NULL;
}
//Run
if(count($ARRAY)>0 and $r_list == $list and $r_address == $address and $r_disabled == "false" and $r_comment == $comment) {
return(false);
} elseif(count($ARRAY)>0){
$API->write('/ip/firewall/address-list/set',false);
$API->write("=.id=".$ARRAY[0]['.id'],false);
$API->write('=comment='.$comment,false);
//$API->write('=address='.$address,false);
$API->write('=disabled=no',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
return(true);
} else {
$API->write('/ip/firewall/address-list/add',false);
$API->write('=list='.$list,false);
$API->write('=comment='.$comment,false);
$API->write('=address='.$address,true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
return(true);
}
}