imcrazytwkr

RouterOS multiWAN gateway script

Jan 3rd, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. {
  2. :local rname "rt:main:out"
  3. :local rfname "rt:failover:out"
  4. :local lname "rt:main:lan"
  5. :local lfname "rt:failover:lan"
  6. :local mtname "rt:mt:out"
  7. :local uname "uplink_rt"
  8.  
  9. :local createRoute do={
  10. :local count [/ip route print count-only where comment=$1]
  11. if ($count=0) do={
  12. # Creating main route
  13. /ip route add distance=$2 gateway=$3 comment=$1 routing-table=$4 check-gateway=$5
  14. }
  15. }
  16.  
  17. :local updateRoute do={
  18. :local route [/ip route find where comment=$1]
  19. :if ([/ip route get $route gateway] != $2) do={
  20. /ip route set $route gateway=$2
  21. }
  22. }
  23.  
  24. :if ($bound=1) do={
  25. :local rcount [/ip route print count-only where comment=$rname]
  26.  
  27. # Main route
  28. :if ($rcount=0) do={
  29. $createRoute $rname 1 $"gateway-address" "rout_ISP1" "ping"
  30. } else={
  31. :if ($rcount>1) do={
  32. :error "Multiple main routes found"
  33. }
  34.  
  35. $updateRoute $rname $"gateway-address"
  36. }
  37.  
  38. # Fallback route
  39. :local rfcount [/ip route print count-only where comment=$rfname]
  40. :if ($rfcount=0) do={
  41. $createRoute $rfname 2 $"gateway-address" "rout_ISP2" "none"
  42. } else={
  43. :if ($rfcount > 1) do={
  44. :error "Multiple fallback routes found"
  45. }
  46.  
  47. $updateRoute $rfname $"gateway-address"
  48. }
  49.  
  50. # Main route for LAN
  51. :local lcount [/ip route print count-only where comment=$lname]
  52. :if ($lcount=0) do={
  53. $createRoute $lname 1 $"gateway-address" "lout_ISP1" "ping"
  54. } else={
  55. :if ($lcount > 1) do={
  56. :error "Multiple fallback routes found"
  57. }
  58.  
  59. $updateRoute $lname $"gateway-address"
  60. }
  61.  
  62. # Fallback route for LAN
  63. :local lfcount [/ip route print count-only where comment=$lfname]
  64. :if ($lfcount=0) do={
  65. $createRoute $lfname 2 $"gateway-address" "lout_ISP2" "none"
  66. } else={
  67. :if ($lfcount > 1) do={
  68. :error "Multiple fallback routes found"
  69. }
  70.  
  71. $updateRoute $lfname $"gateway-address"
  72. }
  73.  
  74. :local mtcount [/ip route print count-only where comment=$mtname]
  75. :if ($mtcount=0) do={
  76. $createRoute $mtname 2 $"gateway-address" "main" "none"
  77. } else={
  78. :if ($mtcount > 1) do={
  79. :error "Multiple fallback routes found"
  80. }
  81.  
  82. $updateRoute $mtname $"gateway-address"
  83. }
  84.  
  85. # Updating current IP just in case
  86. /ip firewall address-list remove [/ip firewall address-list find list=$lname]
  87. /ip firewall address-list add list=$uname address=$"lease-address"
  88. } else={
  89. /ip route remove [find comment=$rname]
  90. /ip route remove [find comment=$rfname]
  91.  
  92. /ip route remove [find comment=$lname]
  93. /ip route remove [find comment=$lfname]
  94.  
  95. /ip route remove [find comment=$mtname]
  96. }
  97. }
Add Comment
Please, Sign In to add comment