0

我在通过 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);
    } 
}
4

1 回答 1

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);
    } 
}
于 2018-12-19T13:40:47.850 回答