Skip to content

Instantly share code, notes, and snippets.

@alpominth
Last active June 25, 2024 17:30
Show Gist options
  • Save alpominth/1314be7da682b13277241ceaf0ed9417 to your computer and use it in GitHub Desktop.
Save alpominth/1314be7da682b13277241ceaf0ed9417 to your computer and use it in GitHub Desktop.
rt_table123.sh - Easily create a firewall mark for an additional routing table and expose the IP adress(es) of a network interface to the system
#!/bin/bash
FW_MARK="$((RANDOM%2147483646 + 1))"
if [ "$(ip -4 rule show fwmark ${FW_MARK})" ] || [ "$(ip -6 rule show fwmark ${FW_MARK})" ]; then
while [ "$(ip -4 rule show fwmark ${FW_MARK})" ] || [ "$(ip -6 rule show fwmark ${FW_MARK})" ]; do
FW_MARK="$((RANDOM%2147483646 + 1))"
done
fi
TABLE="$((RANDOM%2147483396 + 1))"
if [ ! "$(ip -4 route show table ${TABLE} 2>/dev/null || echo 1)" = "1" ] || [ ! "$(ip -6 route show table ${TABLE} 2>/dev/null || echo 1)" = "1" ]; then
while [ ! "$(ip route show table ${TABLE} 2>/dev/null || echo 1)" = "1" ] || [ ! "$(ip -6 route show table ${TABLE} 2>/dev/null || echo 1)" = "1" ]; do
TABLE="$((RANDOM%2147483396 + 1))"
done
fi
pause_and_exit() {
echo "Type Q to quit"
read -r EXIT
if [ ! "${EXIT}" = "Q" ]; then
pause_and_exit
else
sleep 1
fi
}
echo 'Add IPv6 route? [y = YES]'
read -r IPV6_CHOOSE
echo 'Select your Network Card or press enter for auto-detection:'
ip link show
read -r IFACE
if [ ! "${IFACE}" ]; then
IFACE="$(ip route get 8.8.8.8 2>/dev/null | grep 'dev' | awk '{ print $5 }')"
fi
if [ ! "${IFACE}" ]; then
echo "No Internet link was found."
exit 1
fi
if [ "${IPV6_CHOOSE}" = y ]; then
if [ ! "$(ip -6 address show ${IFACE} | grep 'inet6')" ]; then
echo 'The selected network interface has IPv6 disabled.'
exit 1
fi
fi
ip -4 route show dev ${IFACE} | grep -v "default" | while read line; do
ip -4 route add ${line} table ${TABLE} dev ${IFACE}
done
if [ "${IPV6_CHOOSE}" = "y" ]; then
ip -6 route show dev ${IFACE} | grep -v "default" | while read line; do
ip -6 route add ${line} table ${TABLE} dev ${IFACE}
done
fi
ip -4 route add blackhole default table ${TABLE} metric 9999
if [ "$(ip -4 route show dev ${IFACE} | grep "default" | head -n 1)" ]; then
ip -4 route show default dev ${IFACE} | while read line; do
ip -4 route add ${line} dev ${IFACE} table ${TABLE} metric 95
done
else
ip route add default table ${TABLE} dev ${IFACE} metric 95
fi
if [ "${IPV6_CHOOSE}" = "y" ]; then
ip -6 route add blackhole default table ${TABLE} metric 9999
if [ "$(ip -6 route show dev ${IFACE} | grep "default" | head -n 1)" ]; then
ip -6 route show default dev ${IFACE} | while read line; do
ip -6 route add ${line} dev ${IFACE} table ${TABLE} metric 95
done
else
ip -6 route add default table ${TABLE} dev ${IFACE} metric 95
fi
else
ip -6 route add blackhole ::/0 table ${TABLE}
fi
ip -4 rule add fwmark ${FW_MARK} table ${TABLE}
ip -6 rule add fwmark ${FW_MARK} table ${TABLE}
ip -4 address show dev ${IFACE} | grep "scope global" | grep -Po '(?<=inet\s)[^\s]*' | cut -d "/" -f 1 | while read line; do
ip -4 rule add from ${line} table ${TABLE}
ip -4 rule add to ${line} table ${TABLE}
done
if [ "${IPV6_CHOOSE}" = "y" ]; then
ip -6 address show dev ${IFACE} | grep "scope global" | grep -Po '(?<=inet6\s)[^\s]*' | cut -d "/" -f 1 | while read line; do
ip -6 rule add from ${line} table ${TABLE}
ip -6 rule add to ${line} table ${TABLE}
done
fi
echo "================================================================================================================="
echo "| Interface is \"${IFACE}\""
echo "| Fwmark is \"${FW_MARK}\" or \"0x$(printf "%x\n" ${FW_MARK})\""
echo "| Table is \"${TABLE}\""
echo "| "
echo "| "
echo "| Endpoint IPs are:"
echo "| "
echo "| IPv4:"
ip -4 address show dev ${IFACE} | grep "scope global" | grep -Po '(?<=inet\s)[^\s]*' | cut -d "/" -f 1 | while read line; do
echo "| ${line}"
done
echo "| "
if [ "${IPV6_CHOOSE}" = "y" ]; then
echo "| IPv6:"
ip -6 address show dev ${IFACE} | grep "scope global" | grep -Po '(?<=inet6\s)[^\s]*' | cut -d "/" -f 1 | while read line; do
echo "| ${line}"
done
echo "| "
fi
echo "================================================================================================================="
echo ""
pause_and_exit
ip -4 route flush table ${TABLE}
ip -4 rule flush table ${TABLE}
ip -6 route flush table ${TABLE}
ip -6 rule flush table ${TABLE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment