Retrieve outbound interface ip by ip route get

This commit is contained in:
Vigilans committed 2022-08-22 17:52:20 +08:00
1 parent aff816b955
commit d9cfddbdf0
1 file changed
+17 -5
+17 -5
View File
@@ -38,10 +38,12 @@ arWanIp4() {
lanIps="$lanIps|(^169\.254\.[0-9]{1,3}\.[0-9]{1,3}$)" lanIps="$lanIps|(^169\.254\.[0-9]{1,3}\.[0-9]{1,3}$)"
lanIps="$lanIps|(^172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}$)" lanIps="$lanIps|(^172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}$)"
lanIps="$lanIps|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)" lanIps="$lanIps|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)"
lanIps="$lanIps|(^100\.(6[4-9]|[7-9][0-9])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.64.x.x - 100.99.x.x
lanIps="$lanIps|(^100\.1([0-1][0-9]|2[0-7])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.100.x.x - 100.127.x.x
case $(uname) in case $(uname) in
'Linux') 'Linux')
hostIp=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev "$lanIps") hostIp=$(ip -o -4 route get 100.64.0.1 | grep -oE 'src [0-9\.]+' | awk '{print $2}' | grep -Ev "$lanIps")
;; ;;
Darwin|FreeBSD) Darwin|FreeBSD)
hostIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | grep -Ev "$lanIps") hostIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | grep -Ev "$lanIps")
@@ -66,10 +68,20 @@ arWanIp6() {
local hostIp local hostIp
if type curl >/dev/null 2>&1; then local lanIps="(^$)|(^::1$)|(^fe[8-9,A-F])"
hostIp=$(curl -s 6.ipw.cn)
else case $(uname) in
hostIp=$(wget -q -O- 6.ipw.cn) 'Linux')
hostIp=$(ip -o -6 route get 100::1 | grep -oE 'src [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
;;
esac
if [ -z "$hostIp" ]; then
if type curl >/dev/null 2>&1; then
hostIp=$(curl -s 6.ipw.cn)
else
hostIp=$(wget -q -O- 6.ipw.cn)
fi
fi fi
if [ -z "$hostIp" ]; then if [ -z "$hostIp" ]; then