How to ping multiple IP addresses using Python script

Introduction

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 the ping has been received. You can check this shot for more on ping.

A problem to consider is how to go about pinging a pool of hosts on a network. Pinging the hosts one after the other would be difficult. In this shot, we’ll show you how you can ping a pool of hosts using a Python script.

Pinging multiple IPs in Python

You can easily ping multiple hosts with a Python script using the following steps.

Step 1

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

Example

ip_ping.py is the script. ip_list.txt is the 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 on the Notepad. Add the list of IP addresses you wish to ping, starting each on a new line.

Example

ip_list_txt

191.198.174.192 
216.58.223.206 
wikipedia.org

The above 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

  • Open the .py file you have created and add the following codes, as shown below.
main.py
ip_output.txt
ip_list.txt
import os
with open("ip_list.txt") as file:
park = file.read()
park = park.splitlines()
print(" {park} \n")
# ping for each ip in the file
for ip in park:
response = os.popen(f"ping -c 4 {ip} ").read()
# Pinging each IP address 4 times
#saving some ping output details to output file
if("Request timed out." or "unreachable") in response:
print(response)
f = open("ip_output.txt","a")
f.write(str(ip) + ' link is down'+'\n')
f.close()
else:
print(response)
f = open("ip_output.txt","a")
f.write(str(ip) + ' is up '+'\n')
f.close()
# print output file to screen
with open("ip_output.txt") as file:
output = file.read()
f.close()
print(output)
with open("ip_output.txt","w") as file:
pass

Explanation

  • Line 1: We the import OS module.

  • Line 4: We use the with keyword to call the open() function. The complete name of the file is provided as a parameter. The details of the file are saved in the park variable, which is looped through using the for loop on line 9.

  • Lines 9, 10, and 14: As the loop continues, we place an if else condition on lines 9 and 14. This is done to check for some words in the response variable created in line 10 , in order to hold the result of the ping command. On return of true, we check 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 the 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 the code on line 25, and erased afterwards on line 29.

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

Output

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

Successful output

Free Resources