2
votes

I am working on Mac OS and am able to sort a text file consisting of a few IP addresses not in sequential order. I am able to sort the addresses in order and print them.

I would like to also take that new sorted list and ping the addresses in that same order.

Upon running the script, the addresses print out in order, however the scanning starts at the last address in the text file.

Code:

#!/usr/bin/env python3

import subprocess

with open("path to text file") as f:
    lines = f.readlines()

    for target in sorted(lines, key=lambda target:
    (int(target.split(".")[1]))):
        print(target)
    print('-' * 40)
    print('Attempting to ping IP Addresses in order... \n')

for line in target:

    try:
        ping = subprocess.Popen(
            ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1", line],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE
        )
    finally:
        reach, error = ping.communicate()
        print(reach)

For example, if the text file included the following addresses:
10.4.1.1,
10.6.1.1,
10.8.1.1,
10.20.1.1

I would expect the ping results to print out those addresses in that order.

Below is what the script outputs:
10.4.1.1

10.6.1.1

10.8.1.1

10.20.1.1


Attempting to ping IP Addresses in order...

PING 1 (0.0.0.1): 1 data bytes\n\n--- 1 ping statistics ---\n1 packets transmitted, 0 packets received, 100.0% packet loss\n'
b'PING 0 (0.0.0.0): 1 data bytes\n\n--- 0 ping statistics ---\n1 packets transmitted, 0 packets received, 100.0% packet loss\n'
b'' b'PING 2 (0.0.0.2): 1 data bytes\n\n--- 2 ping statistics ---\n1 packets transmitted, 0 packets received, 100.0% packet loss\n'
b'PING 0 (0.0.0.0): 1 data bytes\n\n--- 0 ping statistics ---\n1 packets transmitted, 0 packets received, 100.0% packet loss\n'
b'' b'PING 1 (0.0.0.1): 1 data bytes\n\n--- 1 ping statistics ---\n1 packets transmitted, 0 packets received, 100.0% packet loss\n'
b'' b'PING 1 (0.0.0.1): 1 data bytes\n\n--- 1 ping statistics ---\n1 packets transmitted, 0 packets received, 100.0% packet loss\n'
b''

Ideally, I would like the text file addresses to be printed out as they are pinged. I am not sure if this error is due to opening the file in the beginning then using it again to ping or what is going on.

1

1 Answers

1
votes

The error is in the line for line in target:

You are (re-)using the variable target, it contains the last IP address of your sorted list of addresses. ("10.20.1.1").

If you iterate over a string with a for loop you get one character at a time (["1", "0", "2", "0", "1", "1"]) - the ping program tries to convert to the single chars to IP addresses, and cant't reach them.

Try to restructure your script like this (untested pseudo code):

with open(...) as f:
    lines = f.readlines()

addresses = sorted(lines, ...)

print('showing addresses:')

for address in addresses:
    print(address)


print('checking addresses:')

for address in addresses:
    subprocess.Popen(['ping', ..., address], ...)