我已经在分配给这个任务的时间很短的情况下尽力解决了这个问题。
但不幸的是,我认为avahi/msdns/bonjour的windows实现不支持别名(如果我错了,请举例说明如何支持)。
我从avahi网站提供的示例Python脚本开始:
创建:/usr/bin/avahi-announce-alias
让它可执行并填充它的内容。
import avahi, dbus
from encodings.idna import ToASCII
CLASS_IN = 0x01
TYPE_CNAME = 0x05
TTL = 60
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__':
import time, sys, locale
for each in sys.argv[1:]:
name = unicode(each, locale.getpreferredencoding())
publish_cname(name)
try:
while 1: time.sleep(60)
except KeyboardInterrupt:
print "Exiting"
这个脚本处理每个别名的公告,并会一直运行,直到你终止它。
(因此我们需要创建另一个脚本,如下所示)
创建文本文件/etc/avahi/aliases
我们用它来存储每行一个的此机器的别名。
创建目录/etc/avahi/aliases.d/
我实际上还没有在任何我展示的脚本中使用过它,但对于那些有企图心的人来说,你可以看到需要做什么。
这个想法是你可以将别名分组到单独的文本文件中(当你处理apache中的虚拟主机时,这将更加有意义),这是许多*nix上的守护程序应用程序已经提供的功能(apache和apt只是其中两个示例)。
创建/usr/bin/avahi-announce-aliases
使其可执行并填充它。
import os, sys
from subprocess import Popen
def ensure_file (path):
"""
Looks for file at provided path, creates it if it does not exist.
Returns the file.
"""
rfile = None
if not os.path.exists(path) and os.path.isfile(path) :
rfile = open(path,"w+");
print("ensuring file : %s " % path)
print("file ensured : %s " % path)
return rfile
command = '/usr/bin/avahi-announce-alias'
alias_pid_path = "/tmp/avahi-aliases.pid"
alias_file_path = "/etc/avahi/aliases"
alias_file = open(alias_file_path)
if not os.path.exists(alias_pid_path) :
open(alias_pid_path,"w").close()
alias_pid = open(alias_pid_path,"r")
for line in alias_pid :
txt = line.strip('\n')
if len(txt) > 0 :
print("kill %s" % txt )
os.system("kill %s" % txt)
alias_pid.close()
alias_pid = open(alias_pid_path,"w+")
for line in alias_file :
txt = line.strip('\n')
if len(txt) > 0 :
print("publishing : << %s >>" % txt)
process = Popen([command, txt])
alias_pid.write("%s\n" % str(process.pid))
alias_pid.close()
print("done")
这并不是Python编程的巅峰,所以请随意进行改进。
用法
如果我们的主机名是“server”,而avahi主机名是“server.local”,那么我们可以在/etc/avahi/aliases
文本文件中添加额外主机名:
deluge.server.local
username.server.local
accounts.server.local
something-else.server.local
another.hostname.home
但是实际上,我非常确定只要确保它在网络上不存在,你就可以使用任何主机名,这就是为什么我只创建了正常 avahi 主机名的“子域名”。
然后我们运行:
sudo avahi-publish-aliases
我设置这个的主要原因是为了方便在我的笔记本电脑上模拟 Django 和 Drupal 网站开发。
注意事项
我的唯一遗憾是,Bonjour/Avahi 的 Windows 实现不支持此实现公布的别名,它只会看到通常公布的主 avahi 主机名(即上面例子中的 server.local)。