Files
q-tools/web/whatismyip
2019-05-13 10:53:49 +03:00

43 lines
955 B
Bash
Executable File

#!/bin/bash
function helpexit() {
echo "Print your public internet IP address."
echo "-n return as host name."
echo "-l display only local addresses."
exit
}
function locals_only() {
ip -4 -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {printf "%18s %s\n", $4, $2}' | sort -k 2
exit $?
}
for (( i=1; i<=$#; i++ )); do
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "-n" ]] && { NAME=1; continue; }
[[ "${!i}" = "-l" ]] && { LOCAL=1; continue; }
done
if [[ "$LOCAL" -eq 1 ]]; then
locals_only
fi
which dig &> /dev/null || {
echo Install apt dnsutils >&2
exit 1
}
IP=$( dig +short myip.opendns.com @resolver1.opendns.com )
if [[ "$NAME" -eq 1 ]]; then
output=$( dig +short -x "$IP" )
else
output="$IP"
fi
if [[ -z "$output" ]]; then
exit 1
fi
echo "$output"