Certainly! Here’s a Python code snippet that automatically pings all the devices in a network range:

import subprocess
import ipaddress

# Define the network range to scan
network = “192.168.0.0/24”

# Function to ping a host
def ping_host(host):
response = subprocess.run([“ping”, “-n”, “1”, “-w”, “1000”, host], capture_output=True)
if response.returncode == 0:
print(f”{host} is reachable.”)
else:
print(f”{host} is unreachable.”)

# Iterate through the network range and ping each host
for ip in ipaddress.IPv4Network(network):
host = str(ip)
ping_host(host)

 

In this code, we use the subprocess module to execute the ping command. The ipaddress module helps us iterate through the IP addresses in the network range.

You can customize the network variable with your desired network address and subnet mask. The code then defines the ping_host function, which takes a host (IP address) as an argument. It runs the ping command using subprocess.run and captures the output. Depending on the return code, it prints whether the host is reachable or unreachable.

Finally, the code iterates through all the IP addresses in the network range using the ipaddress.IPv4Network function. For each IP address, it converts it to a string and calls the ping_host function to perform the ping operation.

Save the code in a Python file with a .py extension and run it to automatically ping all the devices in the defined network range. The script will display the reachability status for each device.

To check how to run Python code on Laptop check below link :

How to run Python code on Laptop – IP-NETWORK-BASICS (ipnetworkbasics.com)