bash script to monitor external IP query
by jamtat from LinuxQuestions.org on (#5R3EB)
I cobbled together the following bash script that seems to be effective for my use case:
Code:#!/bin/bash
getip=$(dig +short myip.opendns.com @resolver1.opendns.com)
currentip=$(cat /home/user/my-ip.txt)
dig +short myip.opendns.com @resolver1.opendns.com |grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" &&
if [ "$getip" == "$currentip" ]; then
printf $getip >my-ip.txt
else
printf $getip >my-ip.txt && echo "The new external IP is $getip" | mail -s "External IP has changed" myemailaddr@gmail.com
fiAs may be clear, it checks the external IP for my network from one of the connected hosts and sends me an e-mail if the address gets changed. The script runs hourly via cron.
So, some questions. First, this is a second iteration of the script, since the first iteration was returning false positives, i.e., sending me e-mails when the external IP had not, in fact, changed. I think the reason it was doing that is because when the dig command in the variable would fail due to an internet glitch, the script would detect an IP change due to my-ip.txt being blank. To help keep that from happening I added the Code:dig +short myip.opendns.com @resolver1.opendns.com |grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" && at the beginning, right after the variables are declared.
My question about that, since I've just now implemented that modification, is whether it works as I assume? In other words, in my understanding, if that dig command fails, the rest of the script that evaluates the IP gotten with the stored IP (located in my-ip.txt) and/or writes the new IP to a file and mails me the result, the script will not run, true? Another way of saying this would be to say that the script aborts if that command fails, correct?
Finally, since I'm sort of a bash scripting ignoramus, any improvements anyone wishes to suggest will be appreciated. Thanks
Code:#!/bin/bash
getip=$(dig +short myip.opendns.com @resolver1.opendns.com)
currentip=$(cat /home/user/my-ip.txt)
dig +short myip.opendns.com @resolver1.opendns.com |grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" &&
if [ "$getip" == "$currentip" ]; then
printf $getip >my-ip.txt
else
printf $getip >my-ip.txt && echo "The new external IP is $getip" | mail -s "External IP has changed" myemailaddr@gmail.com
fiAs may be clear, it checks the external IP for my network from one of the connected hosts and sends me an e-mail if the address gets changed. The script runs hourly via cron.
So, some questions. First, this is a second iteration of the script, since the first iteration was returning false positives, i.e., sending me e-mails when the external IP had not, in fact, changed. I think the reason it was doing that is because when the dig command in the variable would fail due to an internet glitch, the script would detect an IP change due to my-ip.txt being blank. To help keep that from happening I added the Code:dig +short myip.opendns.com @resolver1.opendns.com |grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" && at the beginning, right after the variables are declared.
My question about that, since I've just now implemented that modification, is whether it works as I assume? In other words, in my understanding, if that dig command fails, the rest of the script that evaluates the IP gotten with the stored IP (located in my-ip.txt) and/or writes the new IP to a file and mails me the result, the script will not run, true? Another way of saying this would be to say that the script aborts if that command fails, correct?
Finally, since I'm sort of a bash scripting ignoramus, any improvements anyone wishes to suggest will be appreciated. Thanks