Skip to content

Instantly share code, notes, and snippets.

@Anye
Last active September 15, 2017 04:37
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 Anye/050c14aee0653d8d4e19536b28f15d5b to your computer and use it in GitHub Desktop.
Save Anye/050c14aee0653d8d4e19536b28f15d5b to your computer and use it in GitHub Desktop.
#!/bin/bash
get_outer_ip() {
ip_addr=$( curl -sS http://echoip.me/raw )
if [[ ! "${ip_addr}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
ip_addr=$( dig +short @resolver1.opendns.com myip.opendns.com )
ip_addr=${ip_addr//\"/}
if [[ ! "${ip_addr}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
ip_addr=$( dig +short @ns1.google.com o-o.myaddr.l.google.com -t txt )
if [[ ! "${ip_addr}" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
ip_addr=$( ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f 1 -d '/' )
fi
fi
fi
echo ${ip_addr}
}
echo $( get_outer_ip )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment