Last active
September 15, 2017 04:37
-
-
Save Anye/050c14aee0653d8d4e19536b28f15d5b to your computer and use it in GitHub Desktop.
shell get external ip,Usage: curl -sS 'https://gist.githubusercontent.com/Anye/050c14aee0653d8d4e19536b28f15d5b/raw/get_outer_ip.sh' | bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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