[H-GEN] Bash script testing for changed IP address randomly fails

Jason Parker-Burlingham jasonp at panix.com
Wed Jan 18 16:55:33 EST 2006


On Wed, Jan 18, 2006 at 09:56:37PM +1000, David Powell (Moondrake) wrote:

> 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.

You're going to be much better served using dynamic DNS or something
similar.

If the host is directly connected to the 'net through an attached ADSL
modem, why not just parse the output of ifconfig?  (Of course, you
shouldn't have a box with no upgrades for the last 6 months directly
connected to the net, anyway.)

How often does the script run?  That is, is it sending a false alarm on
every run, or just some?

> #!/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

Wait, this isn't vaild sh syntax!  Did you edit the script before
sending it?  That's a shooting offense in some countries.

Furthermore, you might want to change how you echo $ip_old and $ip_now
so you can tell if there is preceeding or trailing whitespace, though I
don't see how that is possible with awk's default setting for $FS.

Cheers,
-- 
Jason Parker-Burlingham
<jasonp at panix.com>
(Watch this space)




More information about the General mailing list