Discovering Devices on Your Wi-Fi Network with Python
Written on
Introduction to Wi-Fi Device Discovery
In our increasingly interconnected world, it is vital to identify the devices connected to your local Wi-Fi network. This awareness helps in bolstering security, improving performance, and facilitating better network management.
This article will demonstrate how to utilize Python—specifically in just 20 lines of code—to identify all devices linked to your local Wi-Fi network.
Setting Up the Environment
To start, we will use the Python library called scapy, which provides all the necessary tools to scan our Wi-Fi network for connected devices. First, install and import the library:
pip install scapy
import scapy.all as scapy
Next, we will define two essential functions: scan and display_result. The scan function will identify all devices on the network, while display_result will format and display these findings.
def scan(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast / arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
clients_list = []
for element in answered_list:
clients_list.append({"ip": element[1].psrc, "mac": element[1].hwsrc})return clients_list
After making the request and saving the results in answered_list, we can iterate through this list to gather each device's address, storing the information in clients_list.
The display_result function is straightforward and organizes the output from the scan:
def display_result(results):
print("IP AddressttMAC Addressn")
for client in results:
print(client["ip"] + "tt" + client["mac"])
With these two functions set up, we can specify the target IP address and execute both functions to display all devices connected to the network:
target_ip = "192.168.1.1/24"
display_result(scan(target_ip))
Complete Code in Just 20 Lines
import scapy.all as scapy
def scan(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")
arp_request_broadcast = broadcast / arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False)[0]
clients_list = []
for element in answered_list:
clients_list.append({"ip": element[1].psrc, "mac": element[1].hwsrc})return clients_list
def display_result(results):
print("IP AddressttMAC Addressn")
for client in results:
print(client["ip"] + "tt" + client["mac"])
target_ip = "192.168.1.1/24"
display_result(scan(target_ip))
This simple yet effective program can help protect your data and offer peace of mind. It can also be expanded to notify you when an unfamiliar device connects to your network.
We hope this guide has equipped you with the skills to swiftly scan your Wi-Fi network for connected devices! Feel free to leave comments or questions if you enjoyed this article!
Engage with the Community
Thank you for being part of our community! Before you leave, consider giving a clap for this article and following the writer! Stay tuned for more content!
Understanding Network Connections
This video provides insights on how to view all devices connected to your network, helping you better manage your Wi-Fi.
Identifying Wi-Fi Users
Learn how to determine who is utilizing your Wi-Fi, ensuring you maintain control over your network.