5 # This file is part of avahi.
7 # avahi is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # avahi is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 # License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with avahi; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 import avahi, dbus, gobject, sys, getopt, string
26 except ImportError, e:
29 def usage(retval = 0):
30 print "%s <name> <address>" % sys.argv[0]
33 if len(sys.argv) != 3:
45 if not (group is None):
50 global server, group, name, address
53 print "Adding address '%s' for '%s' ..." % (name, address)
54 group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
55 group.connect_to_signal('StateChanged', entry_group_state_changed)
56 group.AddAddress(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, name, address)
59 def entry_group_state_changed(state):
60 global name, server, n_rename, main_loop
62 if state == avahi.ENTRY_GROUP_ESTABLISHED:
63 print "Address established."
64 elif state == avahi.ENTRY_GROUP_COLLISION:
66 n_rename = n_rename + 1
68 print "ERROR: No suitable name found after %i retries, exiting." % n_rename
72 hn[0] = server.GetAlternativeHostName(hn[0])
73 name = string.join(hn, '.')
74 print "WARNING: Address/host name collision, changing name to '%s' ..." % name
78 main_loop = gobject.MainLoop()
80 bus = dbus.SystemBus()
81 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
87 except KeyboardInterrupt, k: