Skip to content

Instantly share code, notes, and snippets.

@jpmens
Forked from darac/dhcp-event.py
Created June 14, 2012 09:59
Show Gist options
  • Save jpmens/2929392 to your computer and use it in GitHub Desktop.
Save jpmens/2929392 to your computer and use it in GitHub Desktop.
Thingy for updating powerdns backend when stuff changes
#!/usr/bin/env python
import MySQLdb
import syslog
import sys
import pprint
pp = pprint.PrettyPrinter()
mysql_host = "localhost"
mysql_user = "dbusername"
mysql_pass = "dbpassword"
mysql_db = "powerdns"
#ClientIP, ClientMac, host-decl-name
if (len(sys.argv) > 1):
command = sys.argv[1]
clientIP = sys.argv[2]
clientMac = sys.argv[3]
hostname = sys.argv[4]
domain = sys.argv[5]
db = MySQLdb.connect(host=mysql_host, user=mysql_user, passwd=mysql_pass, db=mysql_db)
cursor = db.cursor()
if command == "commit":
syslog.syslog("Leased: %s to %s" % (clientIP, hostname))
cursor.execute("INSERT INTO records
(domain_id,name,type,content,ttl,prio,change_date) VALUES ((SELECT id FROM domains WHERE name=%s LIMIT 1),%s,%s,%s,%s,%s,UNIX_TIMESTAMP(NOW()))", [domain, hostname,"A",clientIP,3600,0])
#pp.pprint(cursor.__dict__)
elif command == "release":
# If you don't want to delete records, don't call this script on release
syslog.syslog("Released: %s from %s" % (clientIP, hostname))
cursor.execute("DELETE FROM records WHERE content = %s AND name = %s", [clientIP,hostname])
#pp.pprint(cursor.__dict__)
elif command == "expiry":
# If you don't want to delete records, don't call this script on expiry
syslog.syslog("Expired: %s from %s" % (clientIP, hostname))
cursor.execute("DELETE FROM records WHERE content = %s AND name = %s", [clientIP,hostname])
#pp.pprint(cursor.__dict__)
cursor.close()
db.commit()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment