Check rule/object first before applying RouterOS PHP API

399 Views Asked by At

I am having a bit of a problem with updating rules on my router via PHP API, how can I make my code first check if there is a difference in the setting for example first check the router if the rule/object is disabled and then apply or do nothing if everything matches perfectly.

Currently whenever my sync script runs, it keeps readding(updating) the rule/object on the router when it doesn't need to because the code already has the same info that is already configured on the router.

Current code:

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

There are 1 best solutions below

0
On BEST ANSWER

I have figured the answer I was looking for

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