Created
August 30, 2021 13:30
-
-
Save lidopaglia/ed343b90b4fbc528c0f2ef46ad091378 to your computer and use it in GitHub Desktop.
example that creates a string of curl commands to sequentially check for wan ip.
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 | |
prog=${0##*/} | |
errlog(){ | |
logger -i -s -t $prog -p error "$2" | |
[ $1 -gt 0 ] && exit $1 | |
} | |
infolog(){ | |
logger -i -t $prog -p info "$1" | |
} | |
# Check multiple api endpoints sequentially. | |
ip_urls=( | |
"https://ipv4.icanhazip.com" | |
"https://api.ipify.org" | |
"https://ifconfig.me" | |
"https://bot.whatismyipaddress.com" | |
"https://ipinfo.io/ip" | |
"https://ipecho.net/plain" | |
) | |
# we use -s to suppress errors and -f to "fail". | |
ip_cmd='curl -sf --dns-servers 1.1.1.1' | |
ip=$(eval \ | |
$(echo ${ip_urls[@]} \ | |
| sed -e "s/\s/ || ${ip_cmd} /g;s/^/${ip_cmd} /")) | |
if [ -z "$ip" ]; then | |
errlog 1 "External IP not found" | |
else | |
infolog "Found external IP: $ip" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment