LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2021, 06:53 PM   #1
jamtat
Member
 
Registered: Oct 2004
Distribution: Debian/Ubuntu, Arch, Gentoo, Void
Posts: 134

Rep: Reputation: 24
bash script to monitor external IP query


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
fi
As 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
 
Old 10-24-2021, 07:54 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,081

Rep: Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441
False, Adding the line will not change execution of the script.

It would be nice to know the value of $getip when dig fails. If it outputs nothing or maybe an error message to stderr then you can try checking for an empty string.

Code:
[ -z "$getip" ] && exit 
if [[ "$getip" != "$currentip" ]]; then
    printf $getip >my-ip.txt && echo "The new external IP is $getip" | mail -s "External IP has changed" myemailaddr@gmail.com
fi
As another option if available would be to use a free dynamic IP service like noip. Then you have a constant URL and do not need to worry about your IP address changing. You do have to run a client on your PC or router.
 
Old 10-24-2021, 08:33 PM   #3
jamtat
Member
 
Registered: Oct 2004
Distribution: Debian/Ubuntu, Arch, Gentoo, Void
Posts: 134

Original Poster
Rep: Reputation: 24
Thanks for your input, michaelk. I'm not in a position to dispute what you say about my assumption that the script will abort if the dig command fails since I'm a bit out of my depth when it comes to scripting. But I did run a test to see whether that might happen by running the following command:
Code:
dig +short myip.opendns.com @resolver1.opendns.com |grep -oE "\b([0-9]{1,3}\.){3}[0-9]\b" && echo "got it"
(the regex was modified to make it not properly detect an ipv4 address). With the correct regex "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b", the subsequent echo command will run and show "got it", but with the modified regex, the echo command does not run. That, then, must be an invalid test to determine whether the rest of the commands following the double ampersand in my script should be expected to run?

Finding what will be the output of the dig command if, for example, the internet cannot temporarily be reached is something I need to test, as you suggest. It will be important to know, as you've pointed out, what sort of output, if any, gets generated.

Last edited by jamtat; 10-24-2021 at 08:34 PM.
 
Old 10-24-2021, 10:48 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,081

Rep: Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441
If the Internet is unavailable and dig can't get an address the error message is written to stderr so $getip should be an empty string.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Query (and maybe a dependent query) on alsamixer. ajaygarg Linux - Newbie 4 12-20-2011 01:31 AM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 08:30 AM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 02:25 AM
non Recursive query and Recursive query prashsharma Linux - Server 1 06-27-2007 10:33 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration