It's a type of Planche
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1KB

  1. #!/usr/bin/env bash
  2. LOCAL_HOSTNAME=${1}
  3. for LOCAL_IP in $(unbound-host ${LOCAL_HOSTNAME} | grep -Po "(([0-9]+\.){3}[0-9]+|([0-9a-f]+::?)+:?[0-9a-f]+)"); do
  4. ROUTE=$(ip route get ${LOCAL_IP} | grep -P "(from|via)" 2>/dev/null)
  5. if [ -z "${ROUTE}" ]; then
  6. echo "No route for ${LOCAL_IP}"
  7. continue
  8. fi
  9. DEV=$(echo ${ROUTE} | grep -Po "(?<=dev\s)[^\s]+")
  10. SRC=$(echo ${ROUTE} | grep -Po "(?<=src\s)[^\s]+")
  11. if [ -z "${DEV}" ]; then
  12. echo "No dev for ${LOCAL_IP}"
  13. continue
  14. fi
  15. if [ -z "${SRC}" ]; then
  16. echo "No src for ${LOCAL_IP}"
  17. continue
  18. fi
  19. if echo ${LOCAL_IP} | grep -q ":"; then
  20. # IPv6
  21. FROM=$(echo ${ROUTE} | grep -Po "(?<=from\s)[^\s]+")
  22. if [ -n "${FROM}" ]; then
  23. echo "Adding ${LOCAL_IP} from ${FROM} dev ${DEV} src ${SRC} metric 100"
  24. ip route add ${LOCAL_IP} from ${FROM} dev ${DEV} src ${SRC} metric 100
  25. fi
  26. else
  27. # IPv4
  28. VIA=$(echo ${ROUTE} | grep -Po "(?<=via\s)[^\s]+")
  29. if [ -n "${VIA}" ]; then
  30. echo "Adding ${LOCAL_IP} via ${VIA} dev ${DEV} src ${SRC} metric 100"
  31. ip route add ${LOCAL_IP} via ${VIA} dev ${DEV} src ${SRC} metric 100
  32. fi
  33. fi
  34. done