#!/usr/bin/python """ A really simple piece of code that i use to track down my dynamics IP (assigned by my isp while using ppp). """ filename = '/home/soif/Perso/Data/lastIp.sqlite' import sys,os,time try: import sqlite except ImportError: print "Please install python sqlite binding first" sys.exit(0) def createTables(cursor): schema = "CREATE TABLE connect ( id INTEGER PRIMARY KEY, ip VARCHAR(27), date VARCHAR );" cursor.execute(schema) print "Database creation: Done" def printLastIps(cursor): cursor.execute('SELECT * FROM connect ORDER BY id DESC LIMIT 15') results = cursor.fetchall() print "=" * 78 print " Last Ips " print "=" * 78 for r in results: print '- %-15s %s' % ( r[1],r[2] ) def getLastIp(cursor): cursor.execute('SELECT * FROM connect ORDER BY id DESC LIMIT 1') r = cursor.fetchall() if len(r) > 0: return r[0][1] return def insertCurrentIp(cursor,ip): sql = 'INSERT INTO connect values(NULL,"%s","%s") ' % (ip,time.ctime()) cursor.execute(sql) print "Inserting of %s: Done" % ip def usage(): print sys.argv[0] print " Arguments:" print " -c : create the database" print " -a ip : add the ip to the db" print " -l : list the last ips" #import pdb;pdb.set_trace() db = sqlite.connect(filename) cursor = db.cursor() if len(sys.argv) == 2 and sys.argv[1] == '-c': createTables(cursor) elif len(sys.argv) == 2 and sys.argv[1] == '-l': printLastIps(cursor) elif len(sys.argv) == 3 and sys.argv[1] == '-a': newIp = sys.argv[2] lastIp = getLastIp(cursor) insertCurrentIp(cursor,newIp) if lastIp != newIp: print "Sending Email" os.system('echo Changing Ip of gateway from %s to: %s | mail -s 'New Ip %s ' -a 'From: jkx@club-internet.fr' jkx@wanadoo.fr' % (lastIp,newIp,newIp)) else: usage() db.commit()
Sample output
============================================================================== Last Ips ============================================================================== - 217.128.96.43 Thu Aug 28 21:33:40 2003 - 193.252.3.209 Wed Aug 27 21:33:43 2003 - 217.128.96.95 Tue Aug 26 21:33:47 2003 - 80.15.151.254 Tue Aug 26 18:53:18 2003 - 80.15.151.107 Tue Aug 26 07:34:39 2003 - 81.48.168.91 Mon Aug 25 07:34:36 2003 - 80.15.151.40 Sun Aug 24 07:34:38 2003 - 81.48.168.41 Sat Aug 23 07:54:41 2003 - 80.15.151.125 Fri Aug 22 07:34:44 2003 - 80.15.151.169 Thu Aug 21 07:34:46 2003 - 193.251.14.187 Wed Aug 20 07:34:49 2003 - 193.251.14.36 Tue Aug 19 07:35:07 2003 - 81.48.168.47 Mon Aug 18 07:35:01 2003 - 80.15.151.187 Sun Aug 17 07:34:58 2003 - 81.48.168.75 Sat Aug 16 07:35:01 2003