[Tutor] python and cron

Timothy M. Brauch tbrauch@mindless.com
Tue Feb 18 21:12:05 2003


I am having a problem, and I'm not sure if it is with Python or cron.

I have a dynamic IP address for my home computer.  Every time I connect, I
get a new IP.  My connection is not the best, and sometimes I am only
connected for an hour, sometimes a day.  My machine automatically
reconnects, so it's not a big deal.  However, I would like to know my IP so
I can ssh or ftp.

I wrote a small Python script to check my IP.  I would like for this script
to run every hour.  I thought a cron job would be perfect, but it does not
seem to work.  I checked my log and there are no errors and I don't get any
errors emailed to me.  The script works fine from command line.

Here is part of my code:
--------------------------------------------------------------
#! /usr/bin/python
import urllib, ftplib, re, time

##Get the IP address from the router:
source = urllib.urlopen('http://192.168.xxx.xxx/st_devic.html')
info = source.read()
source.close

##Clean up any HTML coding
data = re.sub('<.*>|&nbsp;','',info).split()

##The IP address is in field 29
address = data[29]

##here is the html formatting stuff for the published webpage.

##Write the formatted stuff to a file
formatted = open('ip.html', 'w')
formatted.write(html)
formatted.close()

##Open the file and ftp it so I someone can get my IP
new_file = open('ip.html', 'r')

ftp = ftplib.FTP('ftp.domain.com','login','password')
ftp.cwd('private')
ftp.storbinary('STOR index.html', new_file)
ftp.quit()
new_file.close()
-----------------------------------------------------------------

Here is the entry from crontab:
-----------------------------------------------------------------
0 * * * * /var/ftp/getipaddress.py
-----------------------------------------------------------------

All of this is owned and run by root, so it shouldn't be permissions.

Can someone offer some help?

 - Tim