MikroTik PHP API:添加或删除现有IP>防火墙>地址列表

时间:2017-12-28 18:38:44

标签: php scripting mikrotik router-os

我需要PHP API的帮助,以便在地址列表中添加地址(如果它尚不存在)并删除它是否存在。

我正在使用https://wiki.mikrotik.com/wiki/API_PHP_class中的routeros_api.class.php。

我试过这个,删除部分工作正常但是在删除之前没有检查它是否存在但是代码的第一部分没有,它只是在运行时挂起。

if ($datalimitexceeded == "1") {
    $API->write('/ip/firewall/address-list/print', false);
    $API->write('?comment='.$comment, false);
    $API->write('?address='.$site_ip, false);
    $READ = $API->read(false);
    $ARRAY = $API->parseResponse($READ);
    if(count($ARRAY)>0){
        $API->write("/ip/firewall/address-list/add",false);
        $API->write("=.id=".$ARRAY[0]['.id'],false);
        $API->write('=list='.$list,false);
        $API->write('=address='.$site_ip,false);
        $API->write('=comment='.$comment,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,false);
        $READ = $API->read(false);
        $ARRAY = $API->parseResponse($READ);
    }

1 个答案:

答案 0 :(得分:0)

修正并整理了我的问题!

这是我的工作代码:

if ($datalimitexceeded == "1") { 
        $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='.$site_ip,false);
            $API->write('=comment='.$comment,true);
            $READ = $API->read(false);
            $ARRAY = $API->parseResponse($READ);
        } 
    }

    if ($datalimitexceeded == "0") {
        $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/remove', false);
            $API->write('=.id=' . $ARRAY[0]['.id']);
            $READ = $API->read(false);
        }
    }