Advertisement
eibgrad

ddwrt-split-gateway-328595.sh

Mar 18th, 2021 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.84 KB | None | 0 0
  1. #!/bin/sh
  2. # installation: copy/paste this script into shell (telnet/ssh) window
  3.  
  4. WANUP_DIR="/jffs/etc/config"
  5. WANUP_SCRIPT="$WANUP_DIR/alt-gtwy.wanup"
  6.  
  7. mkdir -p $WANUP_DIR
  8.  
  9. cat << "EOF" > $WANUP_SCRIPT
  10. #!/bin/sh
  11. set -x # uncomment/comment to enable/disable debug mode
  12. (
  13. TID='200'
  14.  
  15. ALT_GTWY='192.168.1.88'
  16.  
  17. # source IPs to be routed via 192.168.1.1
  18. SOURCE_IP='
  19. 192.168.3.177
  20. 192.168.3.178
  21. 192.168.3.179
  22. 192.168.3.180
  23. 192.168.3.181
  24. 192.168.3.182
  25. 192.168.3.183
  26. 192.168.3.184
  27. 192.168.3.185
  28. 192.168.3.186
  29. 192.168.3.187
  30. '
  31.  
  32. # required for serialization when reentry is possible
  33. LOCK="/tmp/$(basename $0).lock"
  34. acquire_lock() { while ! mkdir $LOCK >/dev/null 2>&1; do sleep 10; done; }
  35. release_lock() { rmdir $LOCK >/dev/null 2>&1; }
  36.  
  37. # optional (allows recovery period of two minutes)
  38. #sleep 120
  39.  
  40. # wait for *reliable* internet connection
  41. while ! ping -qc1 -w3 8.8.8.8 >/dev/null 2>&1; do sleep 3; done; sleep 10
  42.  
  43. # one instance at a time
  44. acquire_lock
  45.  
  46. # catch premature exit and cleanup
  47. trap 'release_lock; exit 1' SIGHUP SIGINT SIGTERM
  48.  
  49. # cleanup from possible prior execution
  50. {
  51. ip route del   0.0.0.0/1 via $ALT_GTWY
  52. ip route del 128.0.0.0/1 via $ALT_GTWY
  53. while ip rule del from 0/0 to 0/0 table $TID 2>/dev/null; do :; done
  54. ip route flush table $TID
  55. ip route flush cache
  56. } 2>/dev/null
  57.  
  58. # copy main routing table to alternate
  59. ip route | while read route; do ip route add $route table $TID; done
  60.  
  61. # override main routing table w/ alternate gateway
  62. ip route add   0.0.0.0/1 via $ALT_GTWY
  63. ip route add 128.0.0.0/1 via $ALT_GTWY
  64.  
  65. # start split gateway
  66. for ip in $SOURCE_IP; do ip rule add from $ip table $TID; done
  67.  
  68. # force routing system to recognize changes
  69. ip route flush cache
  70.  
  71. # any concurrent instance(s) may now run
  72. release_lock
  73.  
  74. exit 0
  75.  
  76. ) 2>&1 | logger -t $(basename $0)[$$] &
  77. EOF
  78. chmod +x $WANUP_SCRIPT
  79. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement