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
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
.pyfile and two.txtfiles. 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.txtfiles 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
.pyfile you have created and add the following codes, as shown below.
import oswith open("ip_list.txt") as file:park = file.read()park = park.splitlines()print(" {park} \n")# ping for each ip in the filefor 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 fileif("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 screenwith 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
OSmodule. -
Line 4: We use the
withkeyword to call theopen()function. The complete name of the file is provided as a parameter. The details of the file are saved in theparkvariable, which is looped through using theforloop on line 9. -
Lines 9, 10, and 14: As the loop continues, we place an
if elsecondition 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 oftrue, we check theresponseof the code in theifblock, which is executed. Otherwise, the code in theelseblock runs. -
The
ip_output.txtfile is opened in theappend ('a')mode to record the status of each IP address, as indicated by the ping command in theif-elsecondition 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.