| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# |
| 4 |
# Use a TSIG-signed DDNS update to update our hostname-to-address |
| 5 |
# mapping. |
| 6 |
# |
| 7 |
# usage: ddns.py <ip-address> |
| 8 |
# |
| 9 |
# On linux systems, you can automatically update your DNS any time an |
| 10 |
# interface comes up by adding an ifup-local script that invokes this |
| 11 |
# python code. |
| 12 |
# |
| 13 |
# E.g. on my systems I have this |
| 14 |
# |
| 15 |
# #!/bin/sh |
| 16 |
# |
| 17 |
# DEVICE=$1 |
| 18 |
# |
| 19 |
# if [ "X${DEVICE}" == "Xeth0" ]; then |
| 20 |
# IPADDR=`LANG= LC_ALL= ifconfig ${DEVICE} | grep 'inet addr' | |
| 21 |
# awk -F: '{ print $2 } ' | awk '{ print $1 }'` |
| 22 |
# /usr/local/sbin/ddns.py $IPADDR |
| 23 |
# fi |
| 24 |
# |
| 25 |
# in /etc/ifup-local. |
| 26 |
# |
| 27 |
|
| 28 |
import sys |
| 29 |
|
| 30 |
import dns.query |
| 31 |
import dns.tsigkeyring |
| 32 |
import dns.update |
| 33 |
|
| 34 |
# |
| 35 |
# Replace the keyname and secret with appropriate values for your |
| 36 |
# configuration. |
| 37 |
# |
| 38 |
keyring = dns.tsigkeyring.from_text({"keyname.": "NjHwPsMKjdN++dOfE5iAiQ=="}) |
| 39 |
|
| 40 |
# |
| 41 |
# Replace "example." with your domain, and "host" with your hostname. |
| 42 |
# |
| 43 |
update = dns.update.Update("example.", keyring=keyring) |
| 44 |
update.replace("host", 300, "A", sys.argv[1]) |
| 45 |
|
| 46 |
# |
| 47 |
# Replace "10.0.0.1" with the IP address of your master server. |
| 48 |
# |
| 49 |
response = dns.query.tcp(update, "10.0.0.1", timeout=10) |