File containing the current public IP address: why sometimes empty?

206 Views Asked by At

I am still a beginner in script writing with no educational backgroud in this direction (I studied math, but seldom programmed). I just do it for personal necessity, like monitoring things.

I wanted to use dynamic dns since fixed IP is expensive, but my router is double-NAT (OpenWRT behind a fritzbox as a modem router), so I cannot get the public IP using ifconfig. I was using kdig, but then was told that kdig takes a lot of memory or cpu and that might be causing crash of another program. Someone suggested my to use nslookup instead.

So I tried the following:

nslookup myip.opendns.com resolver1.opendns.com | grep -m2 "Address" | tail -n1 | awk  '{print $2}' > /tmp/currentip  


if [ ! -s /tmp/currentip ]; # if currentip does not have more than 0 size (i.e. is 0 size)
then
  cp /tmp/oldip /tmp/currentip
else
  STATUS=$(cmp -s /tmp/currentip /tmp/oldip; echo $?)  

     if [[ $STATUS -ne 0 ]]; then  # if status isn't equal to 0, then execute code
     cat /tmp/currentip | msmtp [email protected] 
     cp /tmp/currentip /tmp/oldip

    fi
fi

The commented-out lines are just for myself to understand what I am doing. The file "currentip" is then used for dynamic DNS as the source of the current public IP. When the IP changes, I get an email with it.

Now, as you see, I set it up so that in case nslookup might fail, yealding an empty content, the file currentip should stay just the same as before in the end, instead of remaining empty.

This script is then executed by cron for every minute. (in OpenWRT: the current version)

However, I get about 4-5 times a day an email with an empty content, immediately followed by another one with the address which is just the same as before. But I thought I excluded that possibility. Could anyone possibly tell me why it's happening? What did I do wrong? It looks like [ ! -s /tmp/currentip ] is not working properly, but I don't know why.... I will appreciate your help!

2

There are 2 best solutions below

2
fish On

this code looks fine , suggested modification

ipinf = $(nslookup myip.opendns.com resolver1.opendns.com | grep -m2 "Address" | tail -n1 | awk  '{print $2}')
echo $ipinf  > /tmp/currentip
echo $ipinf date > /tmp/saveipinfo

When you run it for a day and the problem you mentioned occurs again, you can check it to know what you found

0
jbweld On

I'm not sure if anyone is interested in this topic anymore. I found the solution to my question just now. My script was alright, and my router was also fine. It was myip.opendns.com that was at fault. Usually

nslookup myip.opendns.com resolver1.opendns.com 

spits out

Server:     resolver1.opendns.com
Address:    208.67.222.222:53

Non-authoritative answer:
Name:   myip.opendns.com
Address: 84.135.xxx.xxx

Non-authoritative answer:

as the output (the address with xxx.xxx is mine). I do then

| grep -m2 "Address" | tail -n1 | awk  '{print $2}' > /tmp/currentip 

Then the file currentip just contains my address. But then, when myip.opendns.com doesn't do the job, it spits out

Server:     resolver1.opendns.com
Address:    208.67.222.222:53

Non-authoritative answer:

*** Can't find myip.opendns.com: No answer

So my currentip file is going to contain 208.67.222.222:53 instead of my IP address. Now, what's interesting is, when I then do

cat /tmp/currentip | msmtp [email protected]

I get an empty email. And the culprit is :. I created a short text containing : , then msmtp sends an empty email. So that was the reason why I was getting an empty email, followed by an email containing the IP address, which hasn't changed. I was worried that something might be very wrong with my router, but thank goodness, that isn't the case! Thank you all for your suggestions. Making the one-line command with pipes into steps helped in figuring out what happened.