#! /usr/bin/env python # This script notifies me when my IP address has changed. Intended to be run by # a cron job frequently. # This software is distributed according to the terms of the Gnu General # Public License. See http://www.gnu.org for more information. This software # comes with no warranty whatsoever, implied or otherwise. import os, re, smtplib IPFILE = '/home/user/data/ip' FROMADDR = 'ipcheck@localhost.localdomain' TOADDR = 'user@domain.com' SUBJECT = 'new IP address' #------------------------------------------------------------------------------# def main(): currIP = getCurrIP() storedIP = getStoredIP() if currIP != storedIP: setStoredIP(currIP) sendNewIP(currIP) #------------------------------------------------------------------------------# def getCurrIP(): ifconf = os.popen('/sbin/ifconfig').readlines() p = re.compile('^.*inet addr:((\d{1,3}\.?){4})') return p.match(ifconf[1]).group(1) #------------------------------------------------------------------------------# def getStoredIP(): ipFile = open(IPFILE) storedIP = ipFile.readline() ipFile.close() storedIP = storedIP[0:-1] # kill the \n return storedIP #------------------------------------------------------------------------------# def setStoredIP(ip): ipFile = open(IPFILE, 'w') ipFile.write(ip + "\n") ipFile.close() #------------------------------------------------------------------------------# def sendNewIP(ip): msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (FROMADDR, TOADDR, SUBJECT)) msg = msg + "oobleck's new ip is " + ip svr = smtplib.SMTP(ip) # connecting to smtp on local host svr.sendmail(FROMADDR, TOADDR, msg) svr.quit() #------------------------------------------------------------------------------# main()