Skip to content

Instantly share code, notes, and snippets.

@auyongcheemeng
Last active October 18, 2024 19:52
Show Gist options
  • Save auyongcheemeng/c9221fd5d8222c6bc9de9a275274f2e9 to your computer and use it in GitHub Desktop.
Save auyongcheemeng/c9221fd5d8222c6bc9de9a275274f2e9 to your computer and use it in GitHub Desktop.
IPv4 / IPv6 address change monitoring and notification bash script with Pushover.net notifications, for use with crontab
#!/bin/bash
## IPv4 / IPv6 address change monitoring and notification bash script with Pushover.net notifications
## intended for use with crontab
if [ ! -e ~/ipv4addr.log ] ; then
touch ~/ipv4addr.log
fi
if [ ! -e ~/ipv6addr.log ] ; then
touch ~/ipv6addr.log
fi
currentIPv4=$(cat ~/ipv4addr.log)
grabbedIPv4=$(wget -O - -q -nv --delete-after myipv4.addr.tools)
currentIPv6=$(cat ~/ipv6addr.log)
grabbedIPv6=$(wget -O - -q -nv --delete-after myipv6.addr.tools)
# API keys from pushover.net
PUSHOVER_APP_KEY=[your-application-key]
PUSHOVER_USER_KEY=[your-user-key]
# Test push commandline for testing format changes (uncomment lines for use)
#curl -d token=$PUSHOVER_APP_KEY -d user=$PUSHOVER_USER_KEY -d title="IP Address Change" -d timestamp="$(date +%s)" \
# -d message="[Test push] As of $(date +%c): IP address has changed from [ $currentIPv4 ] to [ $grabbedIPv4 ]" \
# https://api.pushover.net/1/messages.json
if [ "$currentIPv4" != "$grabbedIPv4" ] ;
then
curl -d token=$PUSHOVER_APP_KEY -d user=$PUSHOVER_USER_KEY -d title="IP Address Change" -d timestamp="$(date +%s)" \
-d message="As of $(date +%c): IP address has changed from [ $currentIPv4 ] to [ $grabbedIPv4 ]" \
https://api.pushover.net/1/messages.json
echo $grabbedIPv4 > ~/ipv4addr.log
fi
if [ "$currentIPv6" != "$grabbedIPv6" ] ;
then
curl -d token=$PUSHOVER_APP_KEY -d user=$PUSHOVER_USER_KEY -d title="IP Address Change" -d timestamp="$(date +%s)" \
-d message="As of $(date +%c): IP address has changed from [ $currentIPv6 ] to [ $grabbedIPv6 ]" \
https://api.pushover.net/1/messages.json
echo $grabbedIPv6 > ~/ipv6addr.log
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment