how to remove NAT using php-api ?

hello,

I wanna remove a NAT with specific SRC-Address (with specific IP address) , using php api.

any help is appreciated.

This is easy enough to do.
Is the src-address unique to a single nat or multiple entries?

if possible, post the output of:

/ip firewall nat print

Thanks for the reply,

I need something like this :

$API->write('/ip/firewall/nat', false);
$API->write('=remove=where', false);
$API->write('=src-address=222.222.222.222');

But I have no idea if its correct, would you please help me ?

Regards.

In order to remove an item, you have to retrieve it’s .id using a query first and then issue a remove command.

// ....connect api, etc, etc

// query api for all matching entries
$API->write('/ip/firewall/nat/getall',false);                             // getall ip firewall nat entries
$API->write('?src-address=222.222.222.222',false);                  // where src-address=222.222.222.222
$API->write('=.proplist=.id');                                              // only return the .id (optional)
$READ = $API-read(true);                                                  //read the response and parse it.  $READ is now an array of all the matches

// if there are more than 1 matches, the array $READ will have more than one element
// if there are no matches, $READ will be empty
foreach($READ as $item){                                                 // loop through all the matches
   $API->write('/ip/firewall/nat/remove',false);                       // remove an /ip firewall nat entry
   $API->write('=.id=' . $item['.id']);                                    //  where .id =  the retrieved value.
   $READ = $API->read(true);                                             //  read the response
                                                                                    // to be done: do something with the response
}

(I couldn’t get the forum to nicely space the comments)

I would test this on a non-production device first.

Thank you very much for the great reply,

It worked like a charm!

and one more question please :
Can I use from this code to remove connections (ip firewall connection) ?

Thanks again :slight_smile: