Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to ping multiple IP addresses using Python script



The ping utility is used to check if a host connected to a network is active, if they are reachable, and how fast the link to them is on the network. With this tool, one host on a network can send a pingA 32 bytes or 64 bytes echo reply message to another host on the same network which sends a reply once received. You can check out more on ping in this shot.

There is a simple problem here, in the event that requires you to ping a pool of hosts on a network, how would you go about it? You won’t possibly ping the hosts one after the other. That would be really difficult. In this shot I will show you how you can do this using a Python script.

Pinging multiple IPs in Python

With the following steps you can easily ping multiple hosts with a python script.

Step 1

  • Make sure you have Python up and running on your computer system.
  • Create a .py file and two .txt files. The python file will contain the script while one of the text files will contain your list of IP addresses separated by space and the other text file will be used by the script to save the ping output

Example is my script. ip_list.txt is my IP list file. And finally info_output.txt will hold information from the ping process.

Step 2

  • Open your ip_list.txt files in a text editor preferably notepad and add your list of IP addresses you wish to ping starting each on a new line.



And that is the sample list of IP addresses we shall ping in our example.

Note how we also included a host name, that will work perfectly as well.

Step 3

  • Now open up the .py file you have created and add the following codes as shown below.
import os

with open("ip_list.txt") as file:
    park =
    park = park.splitlines()
    print(f" {park}  \n")
    # ping for each ip in the file
for ip in park:
    response = os.popen(f"ping {ip} ").read()
    #saving some ping output details to output file
    if("Request timed out." or "unreachable") in response:
	    f = open("ip_output.txt","a")
	    f.write(str(ip) + ' link is down'+'\n')
	    f = open("ip_output.txt","a")  
	    f.write(str(ip) + ' is up '+'\n')
    # print output file to screen
with open("ip_output.txt") as file:
    output =
with open("ip_output.txt","w") as file:    


  • In line 1 the OS module is imported.

  • In line 4 using the with keyword, the open() function was called and the complete name of the file was provided as a parameter. The details of the file was saved in the park variable which was looped through using the for loop on line 9.

  • As the looped continues, on line 9 and 14 an if else condition was placed to check for some words in the response variable created in line 10 to hold the result of the ping command. On return of true, we check on the response of the code in the if block, which is executed. Otherwise the code in the else block runs.

  • The ip_output.txt file is opened in append ('a') mode to record the status of each IP address as indicated by the ping command in the if-else condition block. The content of the file is displayed by code on line 25 and also erased afterwards on line 29.

As customary in Python scripts, maintain a consistent tab method.


The image below is an image of a successful run of the code above. Do download, edit, and enjoy your pinging.

Successful output



View all Courses

Keep Exploring