[H-GEN] Bash script testing for changed IP address randomly fails
David Powell (Moondrake)
moondrake at aanet.com.au
Wed Jan 18 06:56:37 EST 2006
Hi All,
I wonder if someone can help with this one - it's got me stumped?
There's an old box (debian 3.0) I administer that runs a db server and
php frontend. The network that the box is on is connected to the web
via adsl with a dynamic IP address. Rather than set up something with
dyndns or such like (which I know I could do) I wrote a script that, via
cron, checks every half hour to see if the IP address has changed - and
if it has sends me an e-mail giving me the new address.
Now, this script has been working great for the last 12 months. But in
the last week it has suddenly started sending me several notification
e-mails a day, even though the IP isn't changing (it changes anywhere
between 1 month to 3 months normally). I haven't upgraded any packages
on the box for the past 6 months (which could mean security hole I guess).
The script is below. When I run it consecutively in a terminal,
sometimes it decides that the ip addresses are different, sometimes it
accepts they are the same, it seems fairly random. I can't figure it
out - any clues?
#!/bin/bash
# Path for script data
D_PATH="/home/moondrake/bin/data"
#get the current IP address for this box and mail it
to="moondrake at aanet.com.au"
subject="Latest IP Address for XXXXXXXXX"
ip_now=$(curl http://checkip.dyndns.org/ 2>/dev/null | grep "Current" |
awk '{print $6}' | sed 's/<\/body><\/html>//')
ip_old=$(cat $D_PATH/ip_address.dat | awk '{print $3}')
echo $ip_old # only here for testing
echo $ip_now # only here for testing
if [ "$ip_now" != "$ip_old" ] //then the IP address has changed
then
echo "Old IP: $ip_old" > $D_PATH/old_ip.dat
echo "New IP: $ip_now" > $D_PATH/ip_address.dat
echo "The IP Address has changed: Sending notification now" # comment
out when done testing
cat $D_PATH/ip_address.dat $D_PATH/old_ip.dat | mail -e -s "$subject"
"$to"
fi
Cheers,
David
--
David Powell
e: moondrake - at - aanet.com.au
More information about the General
mailing list