This simple Python script registers aliases for you own computer. It does this by creating CNAME entries that point back to your hostname. Your hostname is laready published by Avahi (by default).

I've used this to publish several name based Apache virtual hosts on my laptop, when I needed to present them. For added pleasure I've implemented support for international DNS names - although I'm not sure how correct it is, it seems to work for me.

Usage: test.local somethingelse.local æȯè.local

#! /usr/bin/env python

import avahi, dbus
from encodings.idna import ToASCII

# Got these from /usr/include/avahi-common/defs.h
CLASS_IN = 0x01

TTL = 60

def publish_cname(cname):
    bus = dbus.SystemBus()
    server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER),
    group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()),

    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)

def encode_dns(name):
    out = []
    for part in name.split('.'):
        if len(part) == 0: continue
    return '.'.join(out)

def createRR(name):
    out = []
    for part in name.split('.'):
        if len(part) == 0: continue
    return ''.join(out)

if __name__ == '__main__':
    import time, sys, locale
    for each in sys.argv[1:]:
        name = unicode(each, locale.getpreferredencoding())
        # Just loop forever
        while 1: time.sleep(60)
    except KeyboardInterrupt:
        print "Exiting"


  • this script doesn't check for collisions. To do that it needs to connect to signals from Avahi and use a glib loop.
  • Apples Bonjour for Windows seems to not support CNAME records... this is not confirmed yet, but my first test show this. Apart from that, I've not tested with anything else (except localhost Avahi), yet. {update} Bonjour for Mac OS X resolves CNAME's - just confirmed on a Macbook.
  • I hope Avahi will get this feature built in. An 'alias' variable in it's config file would be great.
Last modified 8 years ago Last modified on Oct 17, 2006, 10:17:55 PM