helm/bin/myip

22 lines
656 B
Plaintext
Raw Permalink Normal View History

2017-12-13 20:55:18 -08:00
#!/usr/bin/env zsh
# Echos your local or WAN IP
if [[ $1 == -w ]]; then
wan=1
shift
fi
if [[ -n $wan ]]; then
2017-12-13 20:55:18 -08:00
dig +short myip.opendns.com @resolver1.opendns.com
else
IF=$(netstat -rn | awk '/^0.0.0.0/ {thif=substr($0,74,10); print thif;} /^default.*UG/ {thif=substr($0,65,10); print thif;}')
if command -v ifconfig >/dev/null; then
2017-12-13 20:55:18 -08:00
ifconfig ${NET_IF} | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
elif command -v ip >/dev/null; then
2017-12-13 20:55:18 -08:00
ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'
else
>&2 echo "No easy way to grab your IP"
exit 1
fi
fi