:if ($pingresultA=0) do={:foreach i in=[/ip route find gateway=$GatewayA] do={/ip route set $i disabled=yes}}
:if ($pingresultA!=0) do={:foreach i in=[/ip route find gateway=$GatewayA] do={:if ([/ip route get $i disabled]) do={/ip route set $i disabled=no}}}
:log info ("tested Gateway $GatewayA")
:delay 5000ms #Test GatewayB
/ip route set [find dst-address="$pingip/32"] disabled=no gateway=$GatewayB comment="Gateway $GatewayB Check"
:local pingresultB [/ping $pingip count=$pingcount]
GatewayB enable/disable:
:if ($pingresultB=0) do={:foreach i in=[/ip route find gateway=$GatewayB] do={/ip route set $i disabled=yes}}
:if ($pingresultB!=0) do={:foreach i in=[/ip route find gateway=$GatewayB] do={:if ([/ip route get $i disabled]) do={/ip route set $i disabled=no}}}
:log info ("tested Gateway $GatewayB")
Can I create a variable with all the gateways inside and create a :foreach clause? Now, if I add another gateway (GatewayC) I have to copy-paste portion of the script at the end and rename the variables. I want something like this:
Because as you can see the gateways are 192.168.1.1, 192.168.2.254 etc which are actually Adsl routers. With check gateway=ping I will only check if the router is turned on, not if it actually has internet.
I would have to create three netwatch profiles (each for every gateway) and use three different ip addresses for monitoring each gateway. Then I would have to create a script to route each ip address through each gateway and THEN call the netwatch to check the gateway. Trust me, this is the easiest way to do it, all in one script. I just want to know if I can create a variable array with all the gateways inside and do a loop for each one. If netwatch supported gateways to ping (such as the ping tool), then I may have used it. (still I would have to create three netwatch profiles for three gateways)
I am trying to do something very similar! Our default route has three gateways and I want to enable or disable them based on the what this script detects about the state of the line.
I guess the problem is that you cannot just disable or enable an actual gateway, you have to add or remove it!
I can get this script working on indiviual routes but it seems to just ignore the route that has multiple gateways!!
No, this script does not add and remove gateways, it disables and enables them. But in order to check every gateway I had to create a static route that forces the ping data through each gateway. So I create a static route, use it to ping a site, then delete it. Then I create another static route, use it to ping the same site and then delete it. If the result of the ping is 0, then I DISABLE all the routes that use the same gateway. If the next time the ping is bigger than zero, then I reanable all the routes with the same gateway.
I see your problem. Unfortunately, the script was not made for that scenario, although I myself use multiple gateways for one route. I will think about it and reply here. I too started with the wiki but either I could not get it to work or it did not suite my needs. That is why I created this script.
Yep the makes perfect sense, I will have to go back to the drawing board because I need it to do per packet routing to the 3 routers addresses and disable (remove or add) that particular gateway (they are ADSL modems), within the one default route rule if it can no longer route to the internet!
I thought setting three routes all with the same distance may also work, but only one route is only ever active!!
I am willing to pay someone if they think they can get this working!!
If you are planning on creating a mangle rule that marks the packets sent to the host you are pinging, remember that you will have to use one host for each gateway you will test, and then, when that gateway is down noone will be able to access that host. So do not use google ot something popular. Then you will have to NOT disable the routes with the routing marks you use for testing along with the default routes.
The onlu solution I can think of to your problem is to create default routes with all the possible combinations. For example, if you have 3 modems you will create route #1 (1-2-3), route#2 (1-2), route#3 (1-3), route#4 (2-3), route#5 (1), route#6 (2), route#7 (3)
Route #1 will have distance 1
Routes #2,3,4 will have distance 2
Routes #5,6,7 will have distance 3
Now you will have to modify my script so it disables any route that CONTAINS the failed gateway. Keep in mind that this was my first script and my knowledge is very limited, so there may be an easier way.
I like your script, although I don’t completely understand what is actually happening at different stages of the script process. my scripting knowledge is poor and all I do is at times make some simple scripts to perform simple actions.
I have one router with two adsl lines attached, but ordered already a third line.
Some Questions:
Does your script now also work for 3 ines? You started the topic with a script for 3 lines, but your ´final´ one checks only two.
In ROSv4.10 I get following while variables are set:
[adminrudy@RB600 ADSL VODAFONE] > #set variables
[adminrudy@RB600 ADSL VODAFONE] > :local pingcount 3
[adminrudy@RB600 ADSL VODAFONE] > :local pingip [:resolve “sex.com”]
[adminrudy@RB600 ADSL VODAFONE] > :local Gateways “10.10.21.1,10.10.22.1”
[adminrudy@RB600 ADSL VODAFONE] > :local Gateway [:toarray $Gateways]
syntax error (line 1 column 27)
[adminrudy@RB600 ADSL VODAFONE] >
The red letter “G” in “$Gateways” is marked when I copy that part of the script into terminal. So this last action is not performed. What is this last action doing anyway? And if of need, how to change the syntax to make it right again?
In next section of the script “Setup Log File” I get same syntax error in ;" :if ([/ip route print count-only where dst-address=“$pingip/32”]=0) do={/ip route add dst-address=($pingip) gateway=(10.10.22.1) comment=“Gateway Check”}; "
Now it is the marked letter p that produces a syntax error
On the “Test Geteway” I get similar syntax errors:
Can you help on this? Does this now mean the script does not work like it should? I can only test it with one down ADSL line. It is dead, no IP traffic while the modem is connected and even adsl signal is there. Technicians are ordered but that can take some time here in Spain.
I had all my routes to both modems enabled to test your script and it does nicely disable the routes through the gateway that is not functioning because the adsl line is down. so it looks to work somewhere. But obviously the syntax errors tell something is not performed well yet…
How do you run the script? Just scheduler? And then every 5 mins or so? Or every minute?
I also have to make a script now that updates my queue tree. The speed limits are separated for each interface, but my download (since it works on the LAN interface) has the combined speed of the two lines. Obviously that has to be changed now for one line.
And when I have this third line it even becomes more complicated. I have to work with three sets of download queues.
Also, I have to make a script that sends the created “Gateways Check.0.txt” file to my mailbox. But actually I need only a message when something really has changed. I don’t want a mail each time the scheduler ran the script. Any help in this?
I noticed one flaw in this setup:
What happens if the test server (www.sex.com) is down? Or just temporarily un pingable?
To avoid shutting a gateway down because the server is down for some small time you should run the script very often.
But that creates lots of log entries and make the router busy with this many times.
Also, from a dedicated WAN loadbalancer brand I experianced that sometimes some sites are just unreachable for ping, while the rest of the internet is perfomring fine. Even google becomes unreachable at times.
So maybe the script should be extended that in case first test to certain server on first gateway fails, the test should use second server (and even third to make it more secure. Three common servers down probably means that internet connection is very bad anyway and can be considered ´down´…
Also, to resolve a web server for the ping test also needs a dns server with the proper data. If the dns server is down, or the cache has the wrong data, the script considers the server unreachable and considers that gateway not working. How to overcome such situation?
Maybe a script that first test the dns server?
Use the final script and add as many gateways as you like, seperated with comma (,). Yes if sex.com (or any other site you use) is down, then the gateways will be disabled. You can use any site you want, just remember that this site will be constantly changing gateways, so do not use e.g facebook.com because you will have connection issues. Just add this script to scheduler and set it for every 2-3 minutes. If you want you can disable the log reporting. I have been running since day 1 and had no problems regarding cpu usage.
You can replace [:resolve “sex.com”] with the ip of your preferred test server. If you are using one isp you can probably find out on of his ip’s (like mail server) that will always be up. This will take care of any dns issues. I just went with [:resolve “xxx.com”] because it was easier to change it for testing purposes.
You cannot run the script line by line in a terminal. You have to create a script (with write access) and run it using the scheduler, or manually. Unfortunately this makes debugging the script very difficult because you cannot see what is happening.
Changing
:if ($pingresult=0) do={:log info (“Gateway $k Down! $pingresult / $pingcount”)} else={:log info (“Gateway $k Up $pingresult / $pingcount”)};
to
:if ($pingresult=0) do={:log info (“Gateway $k Down! $pingresult / $pingcount”)};
will only log down gateways. You will then have to find a script send the file via mail to you. I think I saw somewhere in the forum something like this.
Ok, it seems the script works fine. I finally got my second line back up and the script tested it as up and enabled all routes also pointing to this second line again. All works fine.
I made myself to simple script that enable/disable two scripts that I made to enable/disable a set of Queue trees (I just enable/disable the parent rule, the child becomes automatically void or valid depending on the state of the parent).
But how to build in your script that if one gateway is down one script “QTree_one_adsl” is executed while if the both gateways are up again the script “QTree_two_adsl” is executed?
Please give me some help on this. I really don’t get it how to do it with scripting language.