Skip to content

Instantly share code, notes, and snippets.

@silgon
Created September 27, 2017 15:03
Show Gist options
  • Select an option

  • Save silgon/da78fa20737fd9da1ab0020021d41045 to your computer and use it in GitHub Desktop.

Select an option

Save silgon/da78fa20737fd9da1ab0020021d41045 to your computer and use it in GitHub Desktop.
On IP change with bash
#! /bin/bash
# path to save your ip (it shall always contain only one line, if not it can cause problems)
FILE_PATH=/tmp/myip.txt
# get your IP from an external server (with a regex to verify that it is an ip)
EXT_IP=$(curl http://ipecho.net/plain | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
# if the file doesn't exist, create it
[ ! -f $FILE_PATH ] && touch $FILE_PATH
# if your ip changed, do something
if [ -n "$EXT_IP" ] && [ "$EXT_IP" != "$(cat $FILE_PATH)" ]; then
echo $EXT_IP > $FILE_PATH
echo "action when IP changed"
echo "your new IP is $EXT_IP"
fi
@silgon

silgon commented Sep 27, 2017

Copy link
Copy Markdown
Author

The action can be to send you an email for example. If you have a dynamic IP at your house, it will notify you when you change your IP. This function runs as a cron function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment