0

My current Public IP is DHCP so when it changes I have to reconfigure my L2TP VPN settings on my devices which can be quite annoying if i'm outside the network at the time of change..... Can I setup a bash script on a raspberry pi possibly to ping my public ip from inside my network then send me a email when it changes

Having never done any scripting could anyone provide me with some tutorials or guides on how to do this?

  • Welcome to SO. Please, consider to have a look at https://stackoverflow.com/help/on-topic to get an idea of what kind of questions are discussed here. Try searching the web for "bash tutorial", if you really want to learn how to write shell scripts with bash. – Twonky Dec 19 '19 at 12:35

1 Answers1

0

First, one could use the following snippet to check for your public IP address:

PUBLIC_IP=$(wget -qO- https://ipecho.net/plain ; echo)
export PUBLIC_IP

This can then be stored in a file on disk, and used for comparison next time the script is run.

source ~/.old_ip # file containing previous PUBLIC_IP

if [ "${OLD_IP}" != "${NEW_IP}" ]; then
  echo "Sending email..."
  # Insert command to send email here
fi

Next, see this answer for sending an email, https://stackoverflow.com/a/8260923/8507637.

Finally, I would use cron for scheduling the script to run at specific intervals.

# This example runs every five minutes.
*/5 * * * * /home/jfoley/bin/check_ip.sh
oxr463
  • 1,573
  • 3
  • 14
  • 34