Last active
August 29, 2015 14:03
-
-
Save mariorz/742b1e37be0b27ec5f3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# avahi-alias.py | |
import time | |
import sys | |
import locale | |
import dbus | |
import avahi | |
from encodings.idna import ToASCII | |
# Got these from /usr/include/avahi-common/defs.h | |
CLASS_IN = 0x01 | |
TYPE_CNAME = 0x05 | |
TTL = 3600*2 #seconds | |
def publish_cname(cname): | |
bus = dbus.SystemBus() | |
server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), | |
avahi.DBUS_INTERFACE_SERVER) | |
group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), | |
avahi.DBUS_INTERFACE_ENTRY_GROUP) | |
rdata = createRR(server.GetHostNameFqdn()) | |
cname = encode_dns(cname) | |
group.AddRecord(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), | |
cname, CLASS_IN, TYPE_CNAME, TTL, rdata) | |
group.Commit() | |
def encode_dns(name): | |
out = [] | |
for part in name.split('.'): | |
if len(part) == 0: continue | |
out.append(ToASCII(part)) | |
return '.'.join(out) | |
def createRR(name): | |
out = [] | |
for part in name.split('.'): | |
if len(part) == 0: continue | |
out.append(chr(len(part))) | |
out.append(ToASCII(part)) | |
out.append('\0') | |
return ''.join(out) | |
if __name__ == '__main__': | |
while 1: | |
for each in sys.argv[1:]: | |
name = unicode(each, locale.getpreferredencoding()) | |
publish_cname(name) | |
time.sleep(TTL-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment