diff --git a/web/whatismyip b/web/whatismyip index c44c31c..33ae705 100755 --- a/web/whatismyip +++ b/web/whatismyip @@ -5,23 +5,40 @@ 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 } 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 ifconfig.co/json; exit $? + } + which wget &>/dev/null && { + wget -q -O - ifconfig.co/json; exit $? + } +} + for (( i=1; i<=$#; i++ )); do [[ "${!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 if [[ "$LOCAL" -eq 1 ]]; then locals_only fi +if [[ "$JSON" -eq 1 ]]; then + ifconfig_service +fi + which dig &> /dev/null || { echo Install apt dnsutils >&2 exit 1