prefer no dnstools solutions

This commit is contained in:
Q
2024-03-16 09:28:31 +02:00
parent 2ce8bc5fd8
commit 783a6e1538

View File

@@ -2,25 +2,37 @@
function helpexit() {
echo "Print your public internet IP address."
echo "-n return as host name."
echo "-l display only local addresses."
echo "-j JSON output from ifconfig.co"
exit
echo "Print your public internet IP address."
echo "-n return as host name."
echo "-l display only local addresses."
echo "-j JSON output from ifconfig.co"
exit
}
function locals_only() {
ip -4 -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {printf "%18s %s\n", $4, $2}' | sort -k 2
exit $?
}
function ifconfig_service() {
which curl &>/dev/null && {
curl -4 ifconfig.co/json; exit $?
}
which wget &>/dev/null && {
wget -q -O - ifconfig.co/json; exit $?
}
which jq &>/dev/null && JQ="jq ." || JQ="cat -"
$CURL ifconfig.co/json | $JQ
exit $?
}
function setup_curl() {
which curl &>/dev/null && {
CURL="curl -s -4"
return
}
which wget &>/dev/null && {
CURL="wget -q -O - "
return
}
echo "No curl or wget present" >2
exit 1
}
for (( i=1; i<=$#; i++ )); do
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-h" ]] && helpexit
@@ -31,6 +43,8 @@ for (( i=1; i<=1; i++ )); do
[[ "${!i}" = "-j" ]] && { JSON=1; continue; }
done
setup_curl
if [[ "$LOCAL" -eq 1 ]]; then
locals_only
fi
@@ -39,15 +53,13 @@ if [[ "$JSON" -eq 1 ]]; then
ifconfig_service
fi
which dig &> /dev/null || {
echo Install apt dnsutils >&2
exit 1
}
IP=$( dig +short myip.opendns.com @resolver1.opendns.com )
IP=$( $CURL ifconfig.co/ip )
if [[ -z "$IP" ]]; then
IP=$( curl -s -4 ifconfig.co/ip )
which dig &> /dev/null || {
echo Install apt dnsutils >&2
exit 1
}
IP=$( dig +short myip.opendns.com @resolver1.opendns.com )
fi
if [[ "$NAME" -eq 1 ]]; then