Skip to content

Instantly share code, notes, and snippets.

@paulbdavis
Last active December 14, 2015 22:09
Show Gist options
  • Save paulbdavis/5156219 to your computer and use it in GitHub Desktop.
Save paulbdavis/5156219 to your computer and use it in GitHub Desktop.
Script to get your external ip, with caching (use a cron job to remove the file periodically, or whatever)
#!/usr/bin/env bash
if [ "$1" = "-r" ]
then
rm $HOME/.externalip
exit 0;
fi
cacheFile="$HOME/.externalip"
if [ ! -f "$cacheFile" ] || [ ! "$(cat $cacheFile)" ]
then
wget http://checkip.dyndns.org/ -q -O - | grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>' > "$cacheFile"
fi
echo $(cat "$cacheFile")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment