Monday, April 21, 2025

How to Ping Multiple IP Addresses

import os, time

with open('ip-source.txt') as file:
    dump = file.read()
    dump = dump.splitlines()

    for ip in dump:
        print('Pinging now: ', ip)
        os.system('ping -n 4 {}'.format(ip))
        print('-' * 60)
        time.sleep(5)

This Python script uses the os and time modules to ping a list of IP addresses stored in a text file. The script performs the following actions:

  1. Opens the text file ip-source.txt using the open() function and reads its contents into a string variable dump.
  2. Splits the string dump into a list of IP addresses using the splitlines() method, and stores it in the same variable dump.
  3. Loops through each IP address in dump.
  4. Uses the os.system() function to execute the ping command for each IP address, with the -n option set to 4 to limit the number of ping requests to 4. This sends 4 ICMP packets to the IP address and waits for a response.
  5. Prints a message indicating which IP address is being pinged.
  6. Prints a line of dashes to separate each ping request for readability.
  7. Uses the time.sleep() function to pause the script for 5 seconds before pinging the next IP address.

This script is useful for network administrators who need to check the availability of multiple IP addresses in a network. It automates the process of pinging each IP address and prints the results to the console. By specifying a list of IP addresses in a text file, the script can be easily customized to suit specific network configurations.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...