Skip to content

Instantly share code, notes, and snippets.

@Anye Anye/get_outer_ip.sh
Last active Sep 15, 2017

Embed
What would you like to do?
#!/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
You can’t perform that action at this time.