简体   繁体   中英

Issue with reading/pinging multiple ip address from text file in python

I have just started learning Python and stuck with the following issue. I have a simple program to read ip addresses from a text file and check the ICMP reachability. I don't have issue with the program when there is a single IP address in the text file, however, as soon I add more than 1 IP address in the text file , my program doesn't work. It looks like with more than 1 address, windows ping utility is not even able to understand the IP addresses and tries to resolve name.

Sample code to read the file and check ICMP:

def  validate_ip():
    global ip_add_list

     check = False


    while True:
        try:

            ip_file = raw_input("Enter the file name and extension:")
            selected_ip_file = open(ip_file,'r')
            selected_ip_file.seek(0)
            ip_add_list = selected_ip_file.readlines()
            selected_ip_file.close()

        #print ip_add_list
        except IOError:
            print"*File %s doesn't exist, try again" % ip_file
            continue
    check2 = False
    while True:
        for ip in ip_add_list:
           print ip
           ping_reply = subprocess.call(['ping','-n','5','-w','1000','-a',ip])
           if ping_reply == 0:
               check2 = True
               print "pings completed"


           else:
               check2 = False
               break
    if check2 == True:
        break
    elif check2 == False:
        print"Some or all ip(s) in the file are not reachable, please check and try again"
        validate_ip()

I have a simple text file with the following addresses.

4.2.2.2

8.8.8.8

I can ping these addresses from command prompt ,however not from the program.

This is the errors i get while pinging from the program.

Ping request could not find host 4.2.2.2 . Please check the name and try again. Some or all ip(s) in the file are not reachable, please check and try again

(Looks like it doesn't understand that 4.2.2.2 is already an IP)

As soon I remove the second address from the text file and run again , I am able to ping to 4.2.2.2.

Due to white spaces in the file, IP address was not identified valid in the program. Thanks to cdarke for providing the solution below.

"You might have a newline appended to each IP address. In the subprocess.call try ip.rstrip()"

roadrunner66, I also appreciate your help with this issue and providing the solution.

Note :This question is likely a duplicate of ping-a-site-in-python .

To make code readable turn it into chunks like the ones below. Credit to python-read-file-line-by-line-into-array . The SO question ping-a-site-in-python also discusses alternative ways to ping.

import subprocess

def validate_ips_from_file(filename):
    lines = [line.rstrip('\n') for line in open('filename')]
    validate_ips(lines)

def validate_ips(ipaddresses):
        for ip in ipaddresses:
            ping_reply=validate_ip(ip)
            print ip, ping_reply

def validate_ip(ipaddress):
    ping_reply = subprocess.call(['ping','-c','5','-w','1000','-a',ipaddress])
    #https://en.wikipedia.org/wiki/Ping_(networking_utility)    
    return ping_reply

validate_ips(['www.nytimes.com','www.theregister.co.uk','www.stackoverflow.com'])

output:

www.nytimes.com 1
www.theregister.co.uk 1
www.stackoverflow.com 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM