2

I am trying to add and remove NAT entry using a single line php api code but unable to do it.

Terminal code Mikrotik:

/ip firewall nat remove [find comment=id9]

//Its working fine on terminal

I am trying writing bellow php api:

to Add:

$API->comm("/ip/firewall/nat/add\n=chain=dstnat\n=src-address=103.19.131.3\n=protocol=tcp\n=action=dst-nat\n=comment=id9\n=to-addresses=103.19.130.215\n=to-ports=80");

//Working fine

to remove:

$API->comm("/ip/firewall/nat/remove/[find\n=comment=id9]");

 //Not working

Can anyone help me to solve it? Thanks in advance.

Sakif Ahmed
  • 21
  • 1
  • 4

4 Answers4

0

It seems a typo;

$API->comm("/ip/firewall/nat/remove\n=[find\n=comment=id9]");

Arash
  • 400
  • 4
  • 11
0

I think you must correct the command

$API->comm("/ip/firewall/nat/remove/[/ip/firewall/nat/find\n=comment=id9]");
David L
  • 703
  • 6
  • 22
0

Its done. Bellow is the code:

//Remove Mikrotik NAT using PHP API

$API->write('/ip/firewall/nat/print', false);
$API->write('?comment=id9', false);
$API->write('=.proplist=.id');
$ARRAYS = $API->read();

$API->write('/ip/firewall/nat/remove', false);
$API->write('=.id=' . $ARRAYS[0]['.id']);
$READ = $API->read();

Thanks all for trying.

Sakif Ahmed
  • 21
  • 1
  • 4
0
   if ($API->connect($nas,  $router_username,  $router_password)){
        $API->write('/ip/firewall/nat/getall',false);
        $API->write('?src-address='.$internal_ip.'',false);
        $API->write('=.proplist=.id');
        $READ = $API->read(true);  

        foreach($READ as $item){
        $API->write('/ip/firewall/nat/remove',false);
        $API->write('=.id=' . $item['.id']);
        $READ = $API->read(true);
                                            
        }
        $API->disconnect();     
    }