Sholah Muchsin
Sholah Muchsin

Reputation: 21

Python - PING a list of IP Address from database

Python - PING a list of IP Address from database

I have a list of ip addresses consisting of 200 locations, which in that location there are 4 ip addresses that I need to do ping testing. I intend to make a command which when I write the name or code of a particular location then it will directly ping to 4 ip address at that location. I have learned a bit to create a list that contains the ip address I entered through the command input () like this :

import os
import socket

ip = []
y = ['IP 1 : ','IP 2 : ', 'IP 3 : ', 'IP 4 : ']

while True:
       for x in y:
             server_ip = input(x)
             ip.append(server_ip)
       break

for x in ip:
       print("\n")
       rep = os.system('ping ' + x + " -c 3")

please give me a little advice about the command I want to make so that I no longer need to enter the ip address one by one. which still makes me confused, especially on how to make the existing items in the database into a variable x which we will insert into this command; rep = os.system ('ping' + x + "-c 3")

Upvotes: 0

Views: 2842

Answers (1)

Luis Benavides
Luis Benavides

Reputation: 68

EDIT: It now iterates over a CSV file rather than a hard-coded Python dictionary.

I believe you will be better off using python dictionaries rather than python lists. Assuming you are using Python 3.X, this is what you want to run:

import os
import csv

# Save the IPs you want to ping inside YOURFILE.csv
# Then iterate over the CSV rows using a For Loop
# Ensure your ip addresses are under a column titled ip_address
with open('YOURFILE.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        rep = os.system("ping " + row['ip_address'] + " -c 3")

Upvotes: 1

Related Questions