ip-router.sh 844 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. internetinterface="eth0"
  4. username=`whoami`
  5. if [ "x$username" != "xroot" ] ; then
  6. echo
  7. echo "You must be root in order to run this script..."
  8. echo
  9. exit
  10. fi
  11. if [ "x$1" != "x" ] ; then
  12. internetinterface="$1"
  13. fi
  14. if [ "x$1" == "xdel" ] || [ "x$2" == "xdel" ] ; then
  15. disable="1"
  16. else
  17. disable="0"
  18. fi
  19. if [ "$disable" == "0" ] ; then
  20. echo "Enabling IP forward and setting up masquerade NAT on interface $internetinterface"
  21. echo 1 > /proc/sys/net/ipv4/ip_forward
  22. iptables -t nat -A POSTROUTING -o $internetinterface -j MASQUERADE
  23. else
  24. echo "Disable IP forward and setting down masquerade NAT on interface $internetinterface"
  25. echo 0 > /proc/sys/net/ipv4/ip_forward
  26. iptables -t nat -D POSTROUTING -o $internetinterface -j MASQUERADE
  27. fi