Community discussions

MikroTik App
 
User avatar
panisk0
newbie
Topic Author
Posts: 25
Joined: Sun Mar 06, 2016 10:36 pm
Location: Cracow
Contact:

Failover scrip / another idea

Fri Mar 09, 2018 11:49 pm

foR consideratioN

1. netwatch checks the availability of several hosts
2. the script examines netwatch results and changes routing
3. rest is a patch for routing

/system scheduler
add interval=11s name=Failover-sns on-event="/system script run Failover-sns" policy=read,write,policy,test,romon

/system script
add name=Failover-sns owner=admin policy=read,write,policy,test,romon source="#Safes Failover script v1.6\r\
\n:global WANstatus;\r\
\n\r\
\n{\r\
\n:local myNet [/tool netwatch print count-only where status=down && comment~\"FOsns\"];\r\
\n:if ( \$myNet >= 3 ) do={ \r\
\n :if ( \$WANstatus != \"down\" ) do={ \r\
\n :log error \"Main ISP connection LOST / changing default route\"\r\
\n :foreach i in=[/ip route find where comment=\"Main ISP\" && static] do={/ip route set \$i distance=5}\r\
\n :set WANstatus down\r\
\n }\r\
\n\r\
\n:beep frequency=970 length=3005ms;\r\
\n\r\
\n} else={ \r\
\n :if ( \$WANstatus != \"up\" ) do={ \r\
\n :log warn \"Main ISP connection BACK / changing default route\" \r\
\n :foreach i in=[/ip route find where comment=\"Main ISP\" && static] do={/ip route set \$i distance=1}\r\
\n :set WANstatus up\r\
\n }\r\
\n}\r\
\n}\r\
\n#:log info \$WANstatus"

/tool netwatch
add comment="FOsns: tempus1.gum.gov.pl" down-script=":log info \"netwatch ::: tempus1.gum.gov.pl ::: is down\"" host=194.146.251.100 interval=32s timeout=300ms \
up-script=":log info \"netwatch ::: tempus1.gum.gov.pl ::: is up\""
add comment="FOsns: ntp1.tp.pl" down-script=":log info \"netwatch ::: ntp1.tp.pl ::: is down\"" host=80.50.231.226 interval=31s timeout=100ms up-script=\
":log info \"netwatch ::: ntp1.tp.pl ::: is up\""
add comment="FOsns: ntp.nask.pl" down-script=":log info \"netwatch ::: ntp.nask.pl ::: is down\"" host=195.187.245.55 interval=33s timeout=400ms up-script=\
":log info \"netwatch ::: ntp.nask.pl ::: is up\""
add comment="FOsns: info.cyf-kr.edu.pl" down-script=":log info \"netwatch ::: info.cyf-kr.edu.pl ::: is down\"" host=149.156.4.11 interval=34s timeout=200ms \
up-script=":log info \"netwatch ::: info.cyf-kr.edu.pl ::: is up\""
add comment="FOsns: ntp.icm.edu.pl" down-script=":log info \"netwatch ::: ntp.icm.edu.pl ::: is down\"" host=213.135.59.38 interval=35s timeout=500ms up-script=\
":log info \"netwatch ::: ntp.icm.edu.pl ::: is up\""
add comment="FOsns: www.onet.pl" down-script=":log info \"netwatch ::: www.onet.pl ::: is down\"" host=213.180.141.140 interval=36s timeout=600ms up-script=\
":log info \"netwatch ::: www.onet.pl ::: is up\""

/interface ethernet
set [ find default-name=ether1 ] name=e1_WAN_Master
set [ find default-name=ether2 ] name=e2_WAN_Slave

/interface list
add name=WAN_all
/interface list member
add interface=e1_WAN_Master list=WAN_all
add interface=e2_WAN_Slave list=WAN_all

/ip firewall raw
add action=accept chain=prerouting in-interface-list=WAN_all protocol=icmp src-address=80.50.231.226
add action=drop chain=output dst-address=80.50.231.226 out-interface=e2_WAN_Slave protocol=icmp
add action=accept chain=prerouting in-interface-list=WAN_all protocol=icmp src-address=194.146.251.100
add action=drop chain=output dst-address=194.146.251.100 out-interface=e2_WAN_Slave protocol=icmp
add action=accept chain=prerouting in-interface-list=WAN_all protocol=icmp src-address=195.187.245.55
add action=drop chain=output dst-address=195.187.245.55 out-interface=e2_WAN_Slave protocol=icmp
add action=accept chain=prerouting in-interface-list=WAN_all protocol=icmp src-address=149.156.4.11
add action=drop chain=output dst-address=149.156.4.11 out-interface=e2_WAN_Slave protocol=icmp
add action=accept chain=prerouting in-interface-list=WAN_all protocol=icmp src-address=213.135.59.38
add action=drop chain=output dst-address=213.135.59.38 out-interface=e2_WAN_Slave protocol=icmp
add action=accept chain=prerouting in-interface-list=WAN_all protocol=icmp src-address=213.180.141.140
add action=drop chain=output dst-address=213.180.141.140 out-interface=e2_WAN_Slave protocol=icmp

/ip route
add comment="Second ISP / to_WAN_Slave" distance=1 gateway=172.16.108.1 routing-mark=to_WAN_Slave scope=10
add comment="Second ISP" distance=2 gateway=172.16.108.1
add comment="Main ISP / to_WAN_Master" distance=1 gateway=172.16.56.1 routing-mark=to_WAN_Master
add comment="Main ISP" distance=1 gateway=172.16.56.1

/ip route rule
add dst-address=80.50.231.226/32 table=to_WAN_Master
add dst-address=149.156.4.11/32 table=to_WAN_Master
add dst-address=194.146.251.100/32 table=to_WAN_Master
add dst-address=195.187.245.55/32 table=to_WAN_Master
add dst-address=213.135.59.38/32 table=to_WAN_Master
add dst-address=213.180.141.140/32 table=to_WAN_Master

Who is online

Users browsing this forum: Bing [Bot] and 38 guests