Created
September 27, 2017 15:03
-
-
Save silgon/da78fa20737fd9da1ab0020021d41045 to your computer and use it in GitHub Desktop.
On IP change with bash
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 | |
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
cronfunction.