Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use Multicast DNS (mDNS) on macOS

Multicast DNS (mDNS) is a protocol that allows devices on the same local network to discover each other without the need for a central DNS server. This is particularly useful in environments where devices need to communicate directly, such as in home networks or small office settings. In the Apple ecosystem, mDNS is implemented via Bonjour, a service that facilitates zero-configuration networking.


This article will guide you through the basics of using mDNS on macOS, including how to discover services and devices using built-in tools and commands.


Examples:


1. Discovering Services with Bonjour Browser:
Bonjour Browser is a macOS utility that allows you to discover services advertised via mDNS. It provides a graphical interface to view available services on your local network.



  • Step 1: Download and install Bonjour Browser from a trusted source.

  • Step 2: Launch Bonjour Browser. You will see a list of services being advertised on your local network.

  • Step 3: Click on any service to view more details, such as the device name, IP address, and port number.


2. Using the dns-sd Command:
macOS includes a command-line tool called dns-sd that can be used to interact with mDNS services directly.




  • Discovering Services:
    Open Terminal and run the following command to browse for services of a specific type (e.g., HTTP):


     dns-sd -B _http._tcp

    This command will list all HTTP services available on the local network.




  • Resolving a Service:
    Once you have identified a service, you can resolve it to get more details:


     dns-sd -L <ServiceName> _http._tcp

    Replace <ServiceName> with the actual name of the service you want to resolve.




  • Registering a Service:
    You can also advertise a service on the network using the dns-sd command. For example, to advertise a web server running on port 8080:


     dns-sd -R "My Web Server" _http._tcp . 8080

    This command will make "My Web Server" discoverable on the local network.




3. Using Python with zeroconf Library:
For developers, the zeroconf library in Python provides a way to interact with mDNS services programmatically.




  • Installing zeroconf:


     pip install zeroconf



  • Discovering Services:


     from zeroconf import ServiceBrowser, Zeroconf

    class MyListener:
    def add_service(self, zeroconf, type, name):
    print(f"Service {name} added")

    def remove_service(self, zeroconf, type, name):
    print(f"Service {name} removed")

    zeroconf = Zeroconf()
    listener = MyListener()
    browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener)



  • Registering a Service:


     from zeroconf import ServiceInfo, Zeroconf

    desc = {'path': '/~paulsm/'}
    info = ServiceInfo("_http._tcp.local.",
    "My Web Server._http._tcp.local.",
    addresses=[socket.inet_aton("192.168.1.2")], port=8080,
    properties=desc)

    zeroconf = Zeroconf()
    zeroconf.register_service(info)



To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.