Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created January 11, 2024 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/8f72e30182b383df873669bb5349fee2 to your computer and use it in GitHub Desktop.
Save juanfal/8f72e30182b383df873669bb5349fee2 to your computer and use it in GitHub Desktop.
local and external IP bash resolver
#!/bin/sh
# myip
# juanfc 2024-01-11
# wget http://ipinfo.io/ip -qO -
# ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null
# dig +short myip.opendns.com @208.67.222.222
# dig +short myip.opendns.com @resolver1.opendns.com
# ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null
# https://stackoverflow.com/questions/33743824/how-to-print-my-public-ip-address-in-the-terminal-using-wget
if [[ $# > 0 ]]; then
if [[ $@ = "-h" ]]; then
printf "$0 [-h|anything]\n"
printf "\twithout arguments print external IP\n"
printf "\twith any argument different than -h, the local IP\n"
printf "\twith -h, this help\n"
else
ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null
fi
else
curl https://ipecho.io/plain
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment