ping 如何确定 mDNS 服务的 IP 地址?

ping 如何确定 mDNS 服务的 IP 地址?

我正在尝试编写一个 Python 3 程序,当给定服务名称时,该程序将返回运行该服务的计算机的 IPv4 数字地址。(我不想使用 zeroconf 模块。)

使用https://en.wikipedia.org/wiki/Multicast_DNShttps://pymotw.com/3/socket/multicast.html,我能够将一些东西组合在一起,找到具有 /etc/hosts 条目的主机的 IP 号(即查询 hostname.local)。但是,当我在该系统(hostname.local)上运行注册 _osc._udp.local 的代码时(例如 oscresponder.local),“ping oscresponder.local”返回的 IP 就像“ping hostname.local”一样,但我的代码没有。

换句话说:

ping hostname.local           # works
./mDNS.py hostname.local      # works
ping oscresponder.local       # works
./mDNS.py oscresponder.local  # does not work

那么,ping 所做的事情是否超出了 Wikipedia 文章所列出的范围呢?

(我尝试使用 Wireshark 来找出信息,但是我的网络状况很差,我无法确定 ping 拾取了哪个响应来提供 IPv4 地址。)

PS我的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# An attempt to construct a raw -- and VERY specific -- mDNS query
# from scratch. The adventure begins at:
#
#    https://en.wikipedia.org/wiki/Multicast_DNS
#

import socket
import struct
import sys
import time

def main():

    if len(sys.argv) > 1:
        host, domain = sys.argv[1].split(".")
        host, domain = host.encode(), domain.encode()
        hlen, dlen   = bytes([len(host)]), bytes([len(domain)])
    else:
        print("Usage:\n\n    mdns.py FQDN\n")
        print("No FQDN supplied. Exiting...")
        sys.exit(1)

    # Construct the UDP packet to be multicast
    #    
    PACKET  = b""
    PACKET += b"\x00\x00"              # Transaction ID
    PACKET += b"\x00\x00"              # Flags
    PACKET += b"\x00\x01"              # Number of questions
    PACKET += b"\x00\x00"              # Number of answers
    PACKET += b"\x00\x00"              # Number of authority  resource records
    PACKET += b"\x00\x00"              # Number of additional resource records
    PACKET += hlen                     # Prefixed host name string length
    PACKET += host                     # Host name
    PACKET += dlen                     # Prefixed domain name string length
    PACKET += domain                   # Domain name
    PACKET += b"\x00"                  # Terminator
    PACKET += b"\x00\x01"              # Type (A record, Host address)
    #PACKET += b"\x00\x1C"             # Type (AAAA record, IPv6 address)
    PACKET += b"\x00\x01"              # Class

    print(PACKET)

    multicast_group = ("224.0.0.251", 5353)

    # Create the datagram socket
    #
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Set a timeout so the socket does not block
    # indefinitely when trying to receive data.
    #
    sock.settimeout(0.2)

    # Set the time-to-live for messages to 1 so they do not
    # go past the local network segment.
    #
    ttl = struct.pack('b', 1)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)

    try:
        # Send data to the multicast group
    #   print('sending {!r}'.format(message))
        sent = sock.sendto(PACKET, multicast_group)

        # Look for responses from all recipients
        #
        while True:
            print('waiting to receive')
            try:
                data, server = sock.recvfrom(4096)
            except socket.timeout:
                print('timed out, no more responses')
                break
            else:
                print('received {!r} from {}'.format(
                    data, server))

    finally:
        print('closing socket')
        sock.close()


if __name__ == "__main__":
    main()

相关内容