Boolean displays True when using list but not text file
<p>I am having an issue with getting a ping_reply == 0 when opening a file. When I use a list for the ip_list variable, it has no issue returning 0 (Which 0 represents successful)</p>

<pre><code>import subprocess   


ip_list = []

def ip_is_valid():
    check = False
    #Global exposes outside the local function
    global ip_list
while True:
    #Prompting user for input
    print "\n" + "# " * 20 + "\n"
    ip_file = raw_input("# Enter IP file name followed by extension: ")
    print "\n" + "# " * 20 + "\n"

    #Changing exception message
    try:
        selected_ip_file = open(ip_file, 'r')
        #Start from the beginning of the file
        selected_ip_file.seek(0)

        ip_list = selected_ip_file.readlines()

        selected_ip_file.close()

    except IOError:
        print "\n* File %s does not exist. Please check and try again\n" % ip_file


    for ip in ip_list:
        a = ip.split('.')
        if (len(a) == 4) and (1 &lt;= int(a[0]) &lt;= 223) and (int(a[0]) != 127) and (int(a[0]) != 169 or int(a[1]) != 254) and (0 &lt;= int(a[1]) &lt;= 255 and 0 &lt;= int(a[2]) &lt;= 255 and 0 &lt;= int(a[3]) &lt;= 255):
            check = True
            break
        else:
            print "\n* There was an invalid IP address. Please check and try again.\n"
            check = False
            continue

    if check == False:
        continue
    elif check == True:
        break

check2 = False
    #Check IP Reachability
    print "\n* Checking IP reachability. Please wait...\n"
    while True:
        for ip in ip_list:
            ping_reply = subprocess.call(['ping', '-n', '2', '-w', '2', ip])
            if ping_reply == 0:
                    check2 = True
                    continue
            elif ping_reply == 2:
                    print "\n* No response from device %s." % ip
                    check2 = False
                    break
            else:
                    print "\n* Ping to the following device has failed:", ip
                    check2 = False
                    break


            #Evaluating the check flag  
        if check2 == False:
            print "* Please re-check IP address list or device.\n"
            ip_is_valid()
        elif check2 == True:
            print "\n* All devices are reachable." 
            break
</code></pre>

<p>I get the following error:</p>

<pre><code># # # # # # # # # # # # # # # # # # # #

# Enter IP file name followed by extension: ipaddrlist.txt

# # # # # # # # # # # # # # # # # # # #


* Checking IP reachability. Please wait...

Ping request could not find host 192.168.1.1
. Please check the name and try again.

* Ping to the following device has failed: 192.168.1.1

* Please re-check IP address list or device.
</code></pre>

<p>If I use a list:</p>

<pre><code>Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=2ms TTL=63
Reply from 192.168.1.1: bytes=32 time=2ms TTL=63

Ping statistics for 192.168.1.1:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 2ms, Maximum = 40ms, Average = 21ms

Pinging 192.168.1.2 with 32 bytes of data:
Reply from 192.168.1.2: bytes=32 time=2ms TTL=63
Reply from 192.168.1.2: bytes=32 time=2ms TTL=63

Ping statistics for 192.168.1.2:
    Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 2ms, Maximum = 2ms, Average = 2ms
&gt;&gt;&gt; ping_reply == 0
True
</code></pre>

<p>Try to remove the spaces before and after the ip:</p>

<p><code>ip_file = raw_input("# Enter IP file name followed by extension: ").strip()</code></p>

python networking