我需要一个DNS服务器,主要允许parsing主机名到任何IP。 例如,看起来像ABCD.myhost.example.com主机ABCD.myhost.example.com被parsing为ABCD 。 (源代码,A是0-255等等,可以返回无效的主机名称)
我已经看到,bind9有$GENERATE指令,但它似乎是不可能嵌套它(和2 ^ 32logging可能会破坏服务器或至less消耗大量的内存)。
有没有什么软件可以做到这一点? (可能是支持正则expression式的东西?)
PowerDNS支持多个后端,其中一个是pipe道后端,您可以使用它来处理查询并发送您的请求。 如果添加指令来使用pipe道(在这个例子中,我假设你有一些区域文件,这就是为什么我们launch bind后端。
launch=bind,pipe pipe-command=/etc/pdns/pdns-backend-ip.py pipe-regex=^.*\.host\.example\.com;.*$
然后,您可以使用Python脚本pdns-backend.py来处理查询并发送结果。 类似下面的代码:
#!/usr/bin/python -u # Implementation of a Pipe Backend for PowerDNS # https://doc.powerdns.com/md/authoritative/backend-pipe/ # We need Python unbuffered so we use -u # Another ways to achieve it in # http://stackoverflow.com/questions/107705/disable-output-buffering import sys def build_answer(qname, qtype, answer, ttl=60): return("%s\tIN\t%s\t%d\t1\t%s" % (qname, qtype, ttl, answer)) def send_log(msg): sys.stdout.write("LOG\t%s\n" % msg) def send_data(msg): sys.stdout.write("DATA\t%s\n" % msg) send_log("Sent %s" % msg) # Check first line to ensure that we are properly initialized line = sys.stdin.readline().rstrip() if line != "HELO\t1": sys.stdout.write("FAIL\n") sys.exit(1) sys.stdout.write("OK Python backend is alive!\n") # Process each line! while True: raw_line = sys.stdin.readline() line = raw_line.rstrip() args = line.split("\t") if len(args) < 6: send_log("PowerDNS sent me unparseable line") sys.stdout.write("FAIL\n") continue rtype,qname,qclass,qtype,id,ip = args send_log("Received [[ %s ]]" % line) # PDNS will use the SOA to decide which backend to use. We have to answer only # when the query is as close as possible: # ie: answer host.example.com not to example.com. if qtype == "SOA" and qname.endswith('host.example.com'): send_data(build_answer(qname, 'SOA', 'ns1.example.com ahu.example.com 2008080300 1800 3600 604800 3600')) if qtype in ("A", "ANY") and qname.endswith("host.example.com"): ip_requested = qname.split('.')[0] send_data(build_answer(qname, 'A', ip_requested.replace('-','.'))) sys.stdout.write("END\n")
拥有该DNSconfiguration将允许您将该区域处理为dynamic:
$ host 11-22-33-44.host.example.com 127.0.0.1 Using domain server: Name: 127.0.0.1 Address: 127.0.0.1#53 Aliases: 11-22-33-44.host.example.com has address 11.22.33.44
也许像这样的东西是矫枉过正,很难debugging(示例python脚本没有做任何错误检查),但它可能是一个很好的起点,有一个真正的dynamic和可编程的DNS服务器。