getip and publicip refactored to have not dependencies

used in geip in sshd-dns
master
David Kebler 2024-08-22 10:50:09 -07:00
parent 68bbdccadf
commit bf6e5e85ee
2 changed files with 15 additions and 5 deletions

View File

@ -102,14 +102,24 @@ domain=$(echo $1 | awk -F\. '{print $(NF-1) FS $NF}')
echo "$domain" echo "$domain"
} }
#https://stackoverflow.com/a/44191743
#https://blog.apnic.net/2021/06/17/how-a-small-free-ip-tool-survived/
publicip () { publicip () {
dig +short myip.opendns.com @resolver1.opendns.com # if which curl >/dev/null 2>&1; then if curl ipinfo.io/ip 2> /dev/null; then return; fi fi
# if which dig>/dev/null 2>&1; then if dig +short myip.opendns.com @resolver1.opendns.com 2> /dev/null; then return; fi fi
exec 3<>/dev/tcp/icanhazip.com/80
echo -e 'GET / HTTP/1.0\r\nhost: icanhazip.com\r\n\r' >&3
while read i
do
[ "$i" ] && myip="$i"
done <&3
echo "$myip"
} }
getip () { getip () {
[[ ! $1 ]] && return 1 [[ ! $1 ]] && return 1
if ip=$(host -4 -t A $1); then if ip=$(getent ahostsv4 $1); then
echo $ip | awk '{print $NF}' echo $ip | grep STREAM | '{ print $1 ; exit }'
else else
return 2 return 2
fi fi

View File

@ -9,8 +9,8 @@ local conf
_getip () { _getip () {
[[ ! $1 ]] && return 1 [[ ! $1 ]] && return 1
if ip=$(host -4 -t A $1); then if ip=$(getent ahostsv4 $1); then
echo $ip | awk '{print $NF}' echo $ip | grep STREAM | '{ print $1 ; exit }'
else else
return 2 return 2
fi fi