Skip to content

Instantly share code, notes, and snippets.

@schcriher
Created May 15, 2017 17:20
Show Gist options
  • Save schcriher/e6e3280efc55af5be6ff1b4f43ebeffc to your computer and use it in GitHub Desktop.
Save schcriher/e6e3280efc55af5be6ff1b4f43ebeffc to your computer and use it in GitHub Desktop.
Returns public IP, querying a URL every few minutes at most. If the IP is queried very fast, the cached IP is returned
#!/bin/bash
minute=5
cache=/tmp/public-ip-cached
if [[ ! -f $cache || $(echo "$(stat -c %Y $cache) + $minute * 60 < $(date +%s)" | bc) -eq 1 ]];then
wget http://ipinfo.io/ip -qO - > $cache
fi
cat $cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment