#!/usr/bin/env bash LOCAL_HOSTNAME=${1} for LOCAL_IP in $(unbound-host ${LOCAL_HOSTNAME} | grep -Po "(([0-9]+\.){3}[0-9]+|([0-9a-f]+::?)+:?[0-9a-f]+)"); do ROUTE=$(ip route get ${LOCAL_IP} | grep -P "(from|via)" 2>/dev/null) if [ -z "${ROUTE}" ]; then echo "No route for ${LOCAL_IP}" continue fi DEV=$(echo ${ROUTE} | grep -Po "(?<=dev\s)[^\s]+") SRC=$(echo ${ROUTE} | grep -Po "(?<=src\s)[^\s]+") if [ -z "${DEV}" ]; then echo "No dev for ${LOCAL_IP}" continue fi if [ -z "${SRC}" ]; then echo "No src for ${LOCAL_IP}" continue fi if echo ${LOCAL_IP} | grep -q ":"; then # IPv6 FROM=$(echo ${ROUTE} | grep -Po "(?<=from\s)[^\s]+") if [ -n "${FROM}" ]; then echo "Adding ${LOCAL_IP} from ${FROM} dev ${DEV} src ${SRC} metric 100" ip route add ${LOCAL_IP} from ${FROM} dev ${DEV} src ${SRC} metric 100 fi else # IPv4 VIA=$(echo ${ROUTE} | grep -Po "(?<=via\s)[^\s]+") if [ -n "${VIA}" ]; then echo "Adding ${LOCAL_IP} via ${VIA} dev ${DEV} src ${SRC} metric 100" ip route add ${LOCAL_IP} via ${VIA} dev ${DEV} src ${SRC} metric 100 fi fi done