Python script to ping all IP addresses in a network.

Updated on 17th September 2015

This is a simple and very useful Python script for network administrators to monitor the devices on a network by pinging every IP address on a subnet. On a network that uses DHCP, this script can be used to find out which IP addresses are used and which all are available. This script has been tested on Python version 3.4.3 on Windows platforms. To run this on Linux/Unix platforms just change the parameters for the ping command.

Program Analysis

You need two Python modules - subprocess and ipaddress. The subprocess module lets you spawn new processes on which you run the ping command. The ipaddress module is used to define the network and to get the IP addresses that are available on that network.

At first, the program asks the user to input a network address using the CIDR notation (network address/prefix) format. A network is defined using this information and all available IP addresses in that network is obtained as a list.

The IP addresses are put through a loop and the ping command is executed using the popen constructor of subprocess module. The command output is parsed to check if it contains any of the strings "Destination host unreachable" or "Request timed out" and if found, that IP addresses is marked as offline otherwise it is online.

The ping command in this code limits the number of echo requests to 1 using the -n switch. If you want to run the program on Linux or Unix platform, replace -n with -c. To make the program run faster the timeout for ping response is set to a low value of 500 milliseconds.

Program Source

# Import modules
import subprocess
import ipaddress

# Prompt the user to input a network address
net_addr = input("Enter a network address in CIDR format(ex. ")

# Create the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts on that network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# For each IP address in the subnet, 
# run the ping command with subprocess.popen interface
for i in range(len(all_hosts)):
    output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]
    if "Destination host unreachable" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    elif "Request timed out" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
        print(str(all_hosts[i]), "is Online")

Sample Output

A sample output from the program is shown below

Enter a network address in CIDR format(ex. is Online is Offline is Offline is Offline is Offline is Offline is Offline is Offline is Offline is Online is Offline is Online is Offline is Offline

Post a comment


Your Comment

Email (We dont publish it)


Das Singh | January 4, 2016 4:17 PM |

Can you explain, what folloing code does, each part? "output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]"

patrick | January 4, 2016 4:22 PM |

popen constructor of subprocess module will run the ping command in a new process and get the result through the output pipe

Car | April 19, 2016 2:39 AM |

This did not work at all for me on Debian Linux. I always get this error: Enter a network address in CIDR format(ex. Traceback (most recent call last): File "", line 6, in net_addr = input("") File "", line 1 I changed input to raw_input and decoded as utf-8 but it bombs out later on startup info

chill | August 15, 2016 4:40 AM |

Car, Debian usually comes with Python 2.7. This script is meant for Python 3. I had the same problem with 2.7 but the script works fine on 3.5

Yúlia Kimerling | March 16, 2017 10:42 AM |

It just keep showing, "info = subprocess.STARTUPINFO() AttributeError: 'module' object has no attribute 'STARTUPINFO'" how can I solve this issue???? I really want to see this code working. Thank's!

Yúlia Kimerling | March 16, 2017 10:42 AM |

How do I open a subprocess.popen interface to run the ping code? I really need help with it.