Advertisement
Guest User

MikroTik RouterOS WAN Failover

a guest
Jul 27th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.93 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # MikroTik RouterOS WAN Failover
  3. #-------------------------------------------------------------------------------
  4.  
  5. #-------------------------------------------------------------------------------
  6. # Settings
  7. #-------------------------------------------------------------------------------
  8. :local PRIMARYROUTELABEL "PRIMARY_ROUTE";
  9. :local BACKUPROUTELABEL "BACKUP_ROUTE";
  10. # Hosts to ping via primary route (Yandex.DNS)
  11. :local PRIMARYHOSTS {77.88.8.8; 77.88.8.7};
  12. # Hosts to ping via backup route (Yandex.DNS)
  13. :local BACKUPHOSTS {77.88.8.1; 77.88.8.3};
  14. :local PINGCOUNT 3;
  15. :local PINGTRESHOLD 70;
  16.  
  17. #-------------------------------------------------------------------------------
  18. # Check primary route
  19. #-------------------------------------------------------------------------------
  20. :local primaryOk false;
  21. :local primarySuccessPingCount 0;
  22. :foreach host in=$PRIMARYHOSTS do={
  23.     :local n [/ping $host count=$PINGCOUNT];
  24.     :set primarySuccessPingCount ($primarySuccessPingCount + $n);
  25. }
  26. :set primaryOk (($primarySuccessPingCount * 100) >= ([:len $PRIMARYHOSTS] * $PINGCOUNT * $PINGTRESHOLD));
  27. :if (!$primaryOk) do={
  28.     :log error "Primary internet connection down"
  29. };
  30.  
  31. #-------------------------------------------------------------------------------
  32. # Check backup route
  33. #-------------------------------------------------------------------------------
  34. :local backupOk false;
  35. :local backupSuccessPingCount 0;
  36. :foreach host in=$BACKUPHOSTS do={
  37.     :local n [/ping $host count=$PINGCOUNT];
  38.     :set backupSuccessPingCount ($backupSuccessPingCount + $n);
  39. };
  40. :set backupOk (($backupSuccessPingCount * 100) >= ([:len $BACKUPHOSTS] * $PINGCOUNT * $PINGTRESHOLD));
  41. :if (!$backupOk) do={
  42.     :log error "Backup internet connection down"
  43. };
  44.  
  45. #-------------------------------------------------------------------------------
  46. # Failover
  47. #-------------------------------------------------------------------------------
  48. :local primaryDistance [/ip route get [find comment=$PRIMARYROUTELABEL] distance];
  49. :local backupDistance [/ip route get [find comment=$BACKUPROUTELABEL] distance];
  50. :if ($primaryOk && ($primaryDistance >= $backupDistance)) do={
  51. # Switch to primary route
  52.     /ip route set [find comment=$PRIMARYROUTELABEL] distance=1
  53.     /ip route set [find comment=$BACKUPROUTELABEL] distance=2
  54.     /log warning "Switched to primary internet connection"
  55. # Remove all connections
  56.     /ip firewall connection tracking set enabled=no
  57.     :delay 5s
  58.     /ip firewall connection tracking set enabled=yes
  59. };
  60. :if (!$primaryOk && $backupOk && ($primaryDistance <= $backupDistance)) do={
  61. # Switch to backup route
  62.     /ip route set [find comment=$PRIMARYROUTELABEL] distance=2
  63.     /ip route set [find comment=$BACKUPROUTELABEL] distance=1
  64.     /log error "Switched to backup internet connection"
  65. # Remove all connections
  66.     /ip firewall connection tracking set enabled=no
  67.     :delay 5s
  68.     /ip firewall connection tracking set enabled=yes
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement