eibgrad

merlin-pptp-gw-override.sh

Jul 23rd, 2022 (edited)
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.37 KB | None | 0 0
  1. #!/bin/sh
  2. #DEBUG= # uncomment/comment to enable/disable debug mode
  3.  
  4. #          name: merlin-pptp-gw-override.sh
  5. #       version: 1.0.1, 27-jun-2024, by eibgrad
  6. #       purpose: replace pptp default gateway w/ pptp static routing
  7. #       type(s): init-start
  8. #          href: https://tinyurl.com/2yekbh7e
  9. #  installation:
  10. #    1. enable jffs custom scripts and configs (administration->system)
  11. #    2. ssh to router and copy/paste the following command:
  12. #         curl -kLs bit.ly/merlin-installer|tr -d '\r'|sh -s MLtSBb6E
  13. #    3. modify script w/ your preferred options using nano editor:
  14. #         nano /jffs/scripts/merlin-pptp-gw-override.sh
  15. #    4. reboot
  16.  
  17. SCRIPTS_DIR='/jffs/scripts'
  18. SCRIPT1="$SCRIPTS_DIR/merlin-pptp-gw-override.sh"
  19. SCRIPT2="$SCRIPTS_DIR/init-start"
  20.  
  21. mkdir -p $SCRIPTS_DIR
  22.  
  23. # ---------------------- begin merlin-pptp-gw-override ----------------------- #
  24. cat << 'EOF' > $SCRIPT1
  25. #!/bin/sh
  26. #set -x # comment/uncomment to disable/enable debug mode
  27. {
  28. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  29.  
  30. # remote ip(s)/network(s) reachable over vpn (space separated)
  31. REMOTE_NET='192.168.2.0/24 192.168.3.0/24 192.168.10.100'
  32.  
  33. # how often (in secs) between each check for pptp default gateway
  34. INTERVAL=30
  35.  
  36. # comment/uncomment to enable/disable continous monitoring
  37. STOP_AFTER_SUCCESS=
  38.  
  39. # ------------------------------- END OPTIONS -------------------------------- #
  40.  
  41. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  42.  
  43. while sleep $INTERVAL; do
  44.     VPN_IP="$(nvram get vpnc_ipaddr)"
  45.     VPN_GW="$(nvram get vpnc_gateway)"
  46.     VPN_IF="$(nvram get vpnc_pppoe_ifname)"
  47.  
  48.     # verify appearance of pptp client configuration in nvram
  49.     [[ $VPN_IP && $VPN_GW && $VPN_IF ]] || continue
  50.  
  51.     # verify appearance of pptp client in process table
  52.     pidof 'pppd' &>/dev/null || continue
  53.  
  54.     # verify appearance of pptp client default gateway in routing table
  55.     ip route | grep -Eq   "^0\.0\.0\.0/1\s.*$VPN_IF(\s|$)" || continue
  56.     ip route | grep -Eq "^128\.0\.0\.0/1\s.*$VPN_IF(\s|$)" || continue
  57.  
  58.     # delete pptp client default gateway
  59.     ip route del   0.0.0.0/1 dev $VPN_IF
  60.     ip route del 128.0.0.0/1 dev $VPN_IF
  61.  
  62.     # create static routes to remote ip(s)/network(s) over vpn
  63.     for i in $REMOTE_NET; do
  64.         ip route add $i via $VPN_IP dev $VPN_IF
  65.     done
  66.  
  67.     # force routing system to recognize changes
  68.     ip route flush cache
  69.  
  70.     # continue monitoring or quit?
  71.     [ ${STOP_AFTER_SUCCESS+x} ] && exit 0
  72. done
  73.  
  74. } 2>&1 | logger -t $(basename $0 .sh)[$$]
  75. EOF
  76. [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT1
  77. chmod +x $SCRIPT1
  78. echo "installed: $SCRIPT1"
  79. # ----------------------- end merlin-pptp-gw-override ------------------------ #
  80.  
  81. # ----------------------------- begin init-start ----------------------------- #
  82. create_script() {
  83. cat << 'EOF' > $SCRIPT2
  84. #!/bin/sh
  85. #set -x # comment/uncomment to disable/enable debug mode
  86. {
  87. nohup $SCRIPT1 &>/dev/null &
  88. } 2>&1 | logger -t $(basename $0)[$$]
  89. EOF
  90. [ ${DEBUG+x} ] && sed -ri '2 s/^#(set -x)/\1/' $SCRIPT2
  91. sed "s:\$SCRIPT1:$SCRIPT1:g" -i $SCRIPT2
  92. chmod +x $SCRIPT2
  93. }
  94.  
  95. if [ -f $SCRIPT2 ]; then
  96.     echo "error: $SCRIPT2 already exists; requires manual installation"
  97. else
  98.     create_script
  99.     echo "installed: $SCRIPT2"
  100. fi
  101. # ------------------------------ end init-start ------------------------------ #
Add Comment
Please, Sign In to add comment