Community discussions

MikroTik App
Forum Guru
Forum Guru
Topic Author
Posts: 1726
Joined: Tue Dec 01, 2009 11:46 pm

Ping Script

Thu Dec 31, 2009 5:22 pm

Figured I'd post this script I worked up as it may be useful to other users, and also to see if there is any improvement that can be built into it. I'm basically in a situation where I want to monitor the status of the internet connection to our locations with PCC running on a hotspot. I don't believe that check gateway or netwatch will be the best solutions to this, as check gateway will only watch the default gateway of the router, which does us no good if the router is up but the ISP has an issue further down the line. The problem with netwatch is that you can not specify what interface it uses to test, which is something that I want to do.

I have not yet had a chance to test this on a production router, but I believe that it should work. In order for the script to work, you need to have another default route on the router with a different routing mark, I have this route set up without any checks so that it will hopefully stay up at all times, and it sends the ping out of that route. The router will ping the supplied address 3 times, and if more than 2 fail, it will run again for a total of 3 times. It does this on 3 different supplied IP addresses, and if all 3 fail it will then run the specified operations. It will find any IP address, NAT rule, or route with the supplied comment, it must be the exact comment, but there can be multiple entries with that comment and it will enable/disable them all. Since the route that it is using to test the connection should remain online, once the scrip runs again and the internet connection is back online, it will then go back and undo all of the changes that it made.
:local i 0; {:do {:set i ($i + 1)} while (($i < 3) && ([/ping interval=2 routing-table=ISP1-Test count=3]<=1))};
:local x 0; {:do {:set x ($x + 1)} while (($x < 3) && ([/ping interval=2 routing-table=ISP1-Test count=3]<=1))};
:local y 0; {:do {:set y ($y + 1)} while (($y < 3) && ([/ping interval=2 routing-table=ISP1-Test count=3]<=1))};
:if ($i>=3 && $x>=3 && $y>=3) do={ 
 :log info "ISP1 Down";
 /ip address disable [find comment="ISP1 One-To-One NAT"];
 /ip address enable [find comment="ISP2 One-To-One NAT"];
 /ip firewall nat disable [find comment="ISP1 One-To-One NAT"];
 /ip firewall nat enable [find comment="ISP2 One-To-One NAT"];
 /ip route disable [find comment=ISP1];
} else { :log info "ISP1 UP"; 
 /ip address enable [find comment="ISP1 One-To-One NAT"];
 /ip address disable [find comment="ISP2 One-To-One NAT"];
 /ip firewall nat enable [find comment="ISP1 One-To-One NAT"];
 /ip firewall nat disable [find comment="ISP2 One-To-One NAT"];
 /ip route enable [find comment=ISP1];
Let me know what you think, or if there are any improvements that can be worked into it. Thanks. :D

Edit: Modified it to remove the infinite loop. Also in order for this to work properly you need to disable the check-gateway function inside of "/ip routes". The reason for this is because it will disable any of the same route if one of them has check-gateway enabled.
Frequent Visitor
Frequent Visitor
Posts: 82
Joined: Thu Feb 18, 2010 11:02 am

Re: Ping Script

Tue Mar 09, 2010 5:58 pm

Thank's a lot. Helped :D .
User avatar
MikroTik Support
MikroTik Support
Posts: 6283
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Ping Script

Wed Mar 10, 2010 10:37 am

in one place you use delete in other you use enable with comment ISP1

Who is online

Users browsing this forum: No registered users and 16 guests