Skip to content

Instantly share code, notes, and snippets.

@sheepla
Last active July 23, 2023 11:46
Show Gist options
  • Save sheepla/351dad2512f22c6a4d205525f5911090 to your computer and use it in GitHub Desktop.
Save sheepla/351dad2512f22c6a4d205525f5911090 to your computer and use it in GitHub Desktop.
Lookup my global IP address with shell script (with `dig` command)
#!/bin/sh
_usage() {
cat << EOS
myip - Get global IP address of your computer
USAGE:
myip # will output: NNN.NNN.NNN.NNN
myip [-h|--help] # will output usage message
EOS
}
_has() {
command -v "${1}" &>/dev/null
}
_err() {
printf "[ \e[31;1mERR\e[m ] %s\n" "${1}" >&2
}
_isblank() {
[ -z "${1}" ]
}
if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]
then
_usage
exit 1
fi
if [ "${#}" -ne 0 ]
then
_err "Too many arguments."
_usage
exit 1
fi
if ! _has dig
then
_err "dig command not installed. Please install dig command.
The package name maybe 'bind' or 'bind-utils'."
exit 1
fi
subdomains=("resolver1" "resolver2" "resolver3" "resolver4")
result=""
for s in ${subdomains[@]}
do
_isblank "${result}" || break
result="$(dig +short "myip.opendns.com" "@${s}.opendns.com")"
done
_isblank "${result}" && _err "Could not found "
echo "${result}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment