From 783a6e1538c537f23a1117e0d0b6131071a9ccea Mon Sep 17 00:00:00 2001 From: Q Date: Sat, 16 Mar 2024 09:28:31 +0200 Subject: [PATCH] prefer no dnstools solutions --- web/whatismyip | 58 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/web/whatismyip b/web/whatismyip index 26a86ad..e4b7dfa 100755 --- a/web/whatismyip +++ b/web/whatismyip @@ -2,34 +2,48 @@ 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 + [[ "${!i}" = "--help" ]] && helpexit + [[ "${!i}" = "-h" ]] && helpexit done for (( i=1; i<=1; i++ )); do [[ "${!i}" = "-n" ]] && { NAME=1; continue; } [[ "${!i}" = "-l" ]] && { LOCAL=1; continue; } [[ "${!i}" = "-j" ]] && { JSON=1; continue; } -done +done + +setup_curl if [[ "$LOCAL" -eq 1 ]]; then locals_only @@ -39,18 +53,16 @@ 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 +if [[ "$NAME" -eq 1 ]]; then output=$( dig +short -x "$IP" ) else output="$IP"