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:
- Opens the text file
ip-source.txt
using theopen()
function and reads its contents into a string variabledump
. - Splits the string
dump
into a list of IP addresses using thesplitlines()
method, and stores it in the same variabledump
. - Loops through each IP address in
dump
. - Uses the
os.system()
function to execute theping
command for each IP address, with the-n
option set to4
to limit the number of ping requests to 4. This sends 4 ICMP packets to the IP address and waits for a response. - Prints a message indicating which IP address is being pinged.
- Prints a line of dashes to separate each ping request for readability.
- 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