Remove Nat Sessions on a specific event

Hello to all the community!

I was wondering if there’s a sort of possibility to automatize the removal of specific nat sessions in firewall connection when an event is triggered.
More specific, I have 2 Wan on a Mikrotik with a failover made with routes and distance: when the pppoe goes down on all traffic go on the LTE backup on a different ether interface. What happens is that all sip connections and traffic won’t work unless manually I go on the firewall connections and remove all the 5060 sip connections forcing all the phone to register again.
Is there a way by a script or maybe by a control (maybe netwatch, if is possible in this way my apologies if I posted here) to automatize and do a remove firewall connection every time the default route ( the default wan) changes?
Thanks in advance for your future answers

kindly regards

I use this in both the up and down actions of a netwatch entry that pings my next hop gateway:

/ip firewall connection remove [find];
:log info ("Cleared-Connecitons");

DO NOT WORK as expected

read this:
http://forum.mikrotik.com/t/script-to-clean-firewall-connections-after-public-ip-changes/135275/5

use the second script here, just put on global variables the previous pppoe public IP

http://forum.mikrotik.com/t/script-to-clean-firewall-connections-after-public-ip-changes/135275/5

DO NOT WORK as expected

read this:
http://forum.mikrotik.com/t/script-to-clean-firewall-connections-after-public-ip-changes/135275/5

haha I knew as I wrote that there was something I’d forgotten about this.

Thanks, I’d read your post before; I’d even updated my script with it but for some reason it was missing on the router I copied it from.

Grazie mille!

Grazie a te!

Ciao Rextended, e grazie per la risposta :slight_smile:
I'll try soon your script on my Mikrotik test soon, but I still have a doubt about the second part about the dst address: on your function , it seems to me that you are deleting all nat'ted session matching the $previousIP rule. If this is the case, when I substitute the ip with my isp IP public (static) all connections will not be matched 'cause the dst address is not the one of my wanIP.
Maybe your gateway is a modem or natted with an IP not given by your ISP service? Does your Mikrotik make the pppoe i.e. or you achieve your ip maybe by dhcp from your isp ?
Grazie in anticipo for your future answer :slight_smile:

Chiudi tutte le connessioni NATtate con un indirizzo IP diverso da quello attivo!

:global actualIP 6.7.8.9

/ip fire conn
:foreach idc in=[find where timeout>60 and (!(reply-dst-address~$actualIP))] do={
 remove [find where .id=$idc]
}

Grazie per la risposta :slight_smile:
Can you advise me a correct way to implement this and automatize? I have 2 wans, one with pppoe and ip static from ISP and the other one is a static route to an LTE router without ip public(sim lte). Do you think I have to use the netwatch tool? If yes, I was thinking to insert in the "on down" section the script you posted, but I don't know how to trigger (and which one to choose)the host and how to automatize when the primary wan comes back again.
Grazie ancora, ciao

I’s complicated if more than one connection are used, search for dual wan failover for the implementation.
If DHCP are used on LTE side, the script can be put on dhcp-client script, if LTE have fixed IP on LAN,
netwach one public IP forced by routing to be reachable only on LTE

The dual Wan failover is already implemented, I suppose I have to do a netwatch rule on down to trigger the script, then I have to figure something else for the rollback I suppose..

by the way, it doesn’t work even if I launch it normaly as a simple script tbh

hi adminadmin,

Maybe you can play around with recursive routes to check for connectivity. Basically you set that X IP is reachable through gateway1, and Y IP is reachable through gateway2. Then you create your 0.0.0.0/0 using X and Y as gateways, will be your “testers” and with distance you establish wich one will be primary. In your case it would be something like this:

/ip rou
add check-gateway=ping distance=1 gateway=4.2.2.2 scope=10 target-scope=25
add check-gateway=ping distance=2 gateway=4.2.2.3 scope=10 target-scope=25
add distance=1 dst-address=4.2.2.2/32 gateway=your-pppoe-int scope=25
add distance=1 dst-address=4.2.2.3/32 gateway=your-lte-gw-ip scope=25

In the event that 4.2.2.2 stops responding, by check-gateway it will be declared as unreachable and 4.2.2.3 will take over, rolling back to 4.2.2.2 if it becomes reachable again.

As to delete tracking connections, maybe you can try disabling/enabling interfaces :thinking::thinking: . Never been there, but I’m almost sure that all tracked connections belonging to an interface gets cleared when you disable it.

Regards,

Hi mbaute,
Thank you for your answer. Failover is not a problem here, connectivity turns well when the main WAN goes down. My problem is that when connections go back and forth, old nat session remains and I need to close the old ones ( overall for sip purposes) everytime wan changes.
(Hope I’ve explained the problem, I am driving and writing at the same time ^^')

…NO COMMENT…

Like you have never done that... ^^' :smiley:
Anyway, I've found hot to kill all the session that I need in particular, but still have some trouble to do that just once every time my default change ( failover with different distance)

Oh... how long, last time you were writing and driving at the same time... Did you just get out of the hospital? :laughing: :laughing: :laughing:

Sure, fresh out and still driving texting :stuck_out_tongue:
Returning in topic. I figured out what it can work to solve the problem, but I can't implement with my low knowledge on mikrotik scripts. I need a script that works in a netwatch up/down, that have to kill all session with destination xxx.xxx.xxx.xxx everytime the main connection go down.
my routes atm

/ip route
add distance=10 gateway=10.254.251.254
add distance=22 gateway=192.168.129.254

when 10.254.251.254 is unreachable, the scripts has to /ip firewall connection remove [/ ip firewall connection find dst-address~"xxx.xxx.xxx.xxx:5060"], and do just once.
when 10.254.251.254 comes back reachable, the scripts have to do it once again just one time

Thanks in advance for all the future answers.

create a netwatch for that IP and put on both on-down and on-up this:

/ip fire conn
:foreach idc in=[find where (timeout > 60) and (reply-dst-address ~ "10.254.251.254")] do={
 remove [find where .id=$idc]
}

please do not alter the timeout

http://forum.mikrotik.com/t/dual-wan-failover-script-ping-command/150516/1

I do not write the things without reason.
If just one connection on connection tracking is already closed for timeout (or other reasons) during the execution of the clean,
the script stop with error because when try the connection is already closed, and do not finish his works.

Hi Rextended, how would it be if the connections I need to remove are being natted by a pool, for example a / 30 or / 29?

as I indicate it in your script?