在应用 RouterOS PHP API 之前先检查 rule/object

Check rule/object first before applying RouterOS PHP API

我在通过 PHP API 更新我的路由器规则时遇到了一些问题,我如何让我的代码首先检查设置是否有差异,例如首先如果 rule/object 被禁用,请检查路由器,如果一切都完美匹配,则应用或不执行任何操作。

目前,每当我的同步脚本运行时,它都会在不需要时不断读取(更新)路由器上的 rule/object,因为代码已经具有与路由器上已配置的相同信息。

当前代码:

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);
    } 
}

我找到了我要找的答案

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);
    } 
}