That looks useful and I was bored so I wrote it in python. When I played
with it, I found the server would intermittently return text that loks like:
<html><body><h1> HTTP/1.1 500 Server Error</h1></body></html>
instead of the IP address. So you should be able to add something like
this right after assigning the value to NEWIP
echo $NEWIP|grep "Server Error" 2>/dev/null
if [ "$?" = "0" ]; then
exit
fi
Here's the python version I wrote btw:
#!/usr/bin/env python
import urllib,time
""" Track IP address changes """
CURRENTIP_FILE='CURRENTIP'
GETIP_LOG='getip.log'
def get_current_ip():
""" Connect to whatismyip.com and get the current external IP address """
# This works by connecting to the url, reading a single line and
stripping off any junk whitespace
result=urllib.urlopen('http://automation.whatismyip.com/n09230945.asp
').readline().strip()
# Make sure we got an IP address back instead of a server error message
(an ipv4 address has 4 dots in it)
if len(result.split('.')) == 4:
return result
return None
def get_old_ip(file=CURRENTIP_FILE):
""" Open the file and get the IP address """
try:
OLDIP=open(file,'r').readline().strip()
except IOError:
""" File does not exist """
OLDIP=None
return OLDIP
def save_old_ip(ip,file=CURRENTIP_FILE):
""" Save an IP address to the file """
fh=open(file,'w')
fh.write(ip+'\n')
fh.close()
def log_current_ip(message,file=GETIP_LOG):
""" Log a message to our log file """
fh=open(file,'a')
fh.write(message+'\n')
fh.close()
if __name__ == "__main__":
old=get_old_ip()
new=get_current_ip()
if new and old != new:
print 'IP Changed %s -> %s' % (old,new)
save_old_ip(new)
log_current_ip('%s %s' % (time.asctime(),new))
On Sat, Mar 3, 2012 at 9:35 AM, Rich Shepard <[email protected]>wrote:
> This morning I'm seeing this error when the script looks at
> www.whatismyip.com for the assigned IP address:
>
> /home/rshepard/shell-scripts/ngetip.sh: line 4: [: too many arguments
>
> What is recommended for error checking and graceful recovery in a bash
> shell script like mine? BTW, I can point the browser to that URL and it
> does
> load so I don't know why the script is retuning this error.
>
> mgetip.sh:
>
> #!/bin/bash
> OLDIP=$(cat /home/rshepard/CURRENTIP)
> NEWIP=$(curl -s http://automation.whatismyip.com/n09230945.asp)
> if [ $NEWIP != $OLDIP ]; then # This is line 4
> echo $OLDIP
> echo $NEWIP
> echo $NEWIP > "/home/rshepard/CURRENTIP"
> echo $NEWIP >> "/home/rshepard/getiplog"
> <curl commands removed.>
> fi
>
> TIA,
>
> Rich
>
> _______________________________________________
> PLUG mailing list
> [email protected]
> http://lists.pdxlinux.org/mailman/listinfo/plug
>
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug