Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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:
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.
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.
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)