📖 Scenario: You are setting up a small local network where smart devices can find each other automatically without typing IP addresses. This is useful in homes or offices where devices like printers, smart lights, or speakers need to discover each other easily.
🎯 Goal: Build a simple mDNS (Multicast DNS) setup to discover devices on the local network by their names. You will create a list of devices, configure the mDNS service, implement the discovery logic, and finally display the discovered devices.
📋 What You'll Learn
Create a dictionary called devices with device names as keys and their IP addresses as values
Add a configuration variable called service_type with the value "_http._tcp.local."
Write a function called discover_devices that simulates mDNS discovery by filtering devices matching the service_type
Print the list of discovered device names
💡 Why This Matters
🌍 Real World
mDNS is used in home and office networks to let devices find each other without manual IP setup, making device setup easy and user-friendly.
💼 Career
Understanding mDNS helps in roles involving network configuration, IoT device management, and DevOps tasks related to service discovery and automation.
Progress0 / 4 steps
1
Create the devices dictionary
Create a dictionary called devices with these exact entries: "Printer": "192.168.1.10", "SmartLight": "192.168.1.11", "Speaker": "192.168.1.12"
IOT Protocols
Hint
Use curly braces to create a dictionary with keys as device names and values as IP addresses.
2
Add the service type configuration
Add a variable called service_type and set it to the string "_http._tcp.local." to represent the mDNS service type.
IOT Protocols
Hint
Assign the exact string to the variable service_type.
3
Implement the device discovery function
Write a function called discover_devices that takes devices and service_type as parameters and returns a list of device names. For this simulation, return all device names since all support the service type.
IOT Protocols
Hint
Use list(devices.keys()) to get all device names as a list.
4
Print the discovered devices
Call the discover_devices function with devices and service_type, then print the returned list of device names.
IOT Protocols
Hint
Store the result of discover_devices(devices, service_type) in a variable and print it.
Practice
(1/5)
1. What is the main purpose of mDNS in device discovery?
easy
A. To assign IP addresses to devices on the internet
B. To allow devices to find each other automatically on the same local network
C. To encrypt communication between devices
D. To connect devices over long distances using satellites
Solution
Step 1: Understand mDNS function
mDNS stands for multicast DNS and is used for local network device discovery without a central DNS server.
Step 2: Identify the correct purpose
It helps devices find each other automatically on the same local network using multicast messages and .local names.
Final Answer:
To allow devices to find each other automatically on the same local network -> Option B
Quick Check:
mDNS = local device discovery [OK]
Hint: mDNS is for local network device discovery only [OK]
Common Mistakes:
Confusing mDNS with DHCP for IP assignment
Thinking mDNS encrypts data
Assuming mDNS works over the internet
2. Which of the following is the correct syntax to query a device named printer.local using mDNS on a Linux terminal?
easy
A. mdns-query printer.local
B. mdns-scan printer.local
C. dnslookup printer.local
D. ping printer.local
Solution
Step 1: Identify common mDNS query commands
On Linux, mDNS names like printer.local can be resolved using standard commands like ping or avahi-resolve, but ping is simplest.
Step 2: Check command validity
Only ping printer.local is a valid and common way to test mDNS name resolution; others are not standard commands.
Final Answer:
ping printer.local -> Option D
Quick Check:
Use ping with .local names for mDNS queries [OK]
Hint: Use ping with .local names to test mDNS resolution [OK]
Common Mistakes:
Using non-existent commands like mdns-query
Confusing DNS lookup tools with mDNS
Forgetting the .local suffix
3. Given this Python snippet using zeroconf library to discover mDNS services, what will be printed if a device named 'lamp.local' is found?
from zeroconf import Zeroconf
zeroconf = Zeroconf()
services = zeroconf.get_service_info('_http._tcp.local.', 'lamp._http._tcp.local.')
if services:
print(services.server)
else:
print('No device found')
The get_service_info method returns a ServiceInfo object with a 'server' attribute that ends with a dot, e.g., 'lamp.local.'
Step 2: Analyze the print statement
If the device is found, services.server prints the full hostname with trailing dot, so output is 'lamp.local.'
Final Answer:
lamp.local. -> Option C
Quick Check:
zeroconf server attribute ends with dot [OK]
Hint: zeroconf server names end with a dot '.' [OK]
Common Mistakes:
Omitting the trailing dot in hostname
Confusing service type with hostname
Assuming no device found when device exists
4. You tried to discover devices using mDNS but got no results. Which of these is the most likely cause?
medium
A. The devices are on a different subnet without multicast routing
B. The devices have static IP addresses
C. The devices use HTTPS instead of HTTP
D. The devices have firewalls disabled
Solution
Step 1: Understand mDNS network scope
mDNS works only within the same local network segment (subnet) because multicast packets are not routed across subnets by default.
Step 2: Identify why no devices found
If devices are on a different subnet, mDNS queries won't reach them, causing no discovery results.
Final Answer:
The devices are on a different subnet without multicast routing -> Option A
Quick Check:
mDNS limited to local subnet without routing [OK]
Hint: mDNS only works within the same subnet [OK]
Common Mistakes:
Thinking static IPs block mDNS
Confusing protocol type with discovery issues
Assuming firewalls off always helps discovery
5. You want to set up a smart home device to be discoverable via mDNS with the name smartlight.local. Which configuration step is essential to ensure correct mDNS discovery?
hard
A. Configure the device to advertise its service using the mDNS protocol with the hostname smartlight.local
B. Assign a public IP address to the device
C. Disable multicast on the device's network interface
D. Set the device's DNS server to 8.8.8.8
Solution
Step 1: Understand mDNS advertisement
For a device to be discoverable via mDNS, it must broadcast its hostname and service on the local network using mDNS protocol.
Step 2: Identify correct configuration
Setting the device to advertise as smartlight.local via mDNS is essential; public IP or external DNS settings do not affect local mDNS discovery.
Final Answer:
Configure the device to advertise its service using the mDNS protocol with the hostname smartlight.local -> Option A
Quick Check:
mDNS requires local service advertisement [OK]
Hint: Device must advertise its mDNS hostname locally [OK]
Common Mistakes:
Assigning public IP instead of local mDNS name
Disabling multicast which breaks mDNS
Using external DNS servers for local discovery
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.