Skip to content

Instantly share code, notes, and snippets.

@lidopaglia
Created August 30, 2021 13:30
Show Gist options
  • Save lidopaglia/ed343b90b4fbc528c0f2ef46ad091378 to your computer and use it in GitHub Desktop.
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.
#!/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