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!
this code looks fine , suggested modification
When you run it for a day and the problem you mentioned occurs again, you can check it to know what you found