Home:ALL Converter>Python: Troubleshooting ping script

Python: Troubleshooting ping script

Ask Time:2012-05-17T19:16:52         Author:MHibbin

Json Formatter

I have hit another "wall"... I have the following script, which appears to work fine on Windows, however when I move it to Linux it seems to lose functionality.

Note: I added the print line(s) statements for troubleshooting.

The following script outputs the contents of the hostsFile when I print lines, and then prints each line in turn when I print line. But when it reaches the ping execution, it appears to jump straight to the last host in the file. I was wondering if I have missed something clearly obvious (I am still learning python as a newbie).

import sys, os, platform, subprocess

plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir,'hosts.txt')
hostsFile = open(hosts, "r")
lines = hostsFile.readlines()
print lines
if plat == "Windows":
        for line in lines:
                line = line.strip( )
                ping = subprocess.Popen(
                        ["ping", "-n", "1", "-l", "1", "-w", "100", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
        out, error = ping.communicate()
        print out
        print error

elif plat == "Linux":
        for line in lines:
                print line
                line = line.strip()
                ping = subprocess.Popen(
                        ["ping", "-c", "1", "-s", "1", "-l", "1",line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
        out, error = ping.communicate()
        print out
        print error

hostsFile.close()

Any thoughts/help is appreciated.

Many thanks in advance.

Regards,

MHibbin

EDIT: Thanks to Wooble for the help... the correct code should be (notice the spacing):

import sys, os, platform, subprocess

plat = platform.system()
scriptDir = sys.path[0]
hosts = os.path.join(scriptDir,'hosts.txt')
hostsFile = open(hosts, "r")
lines = hostsFile.readlines()
if plat == "Windows":
        for line in lines:
                line = line.strip( )
                ping = subprocess.Popen(
                        ["ping", "-n", "1", "-l", "1", "-w", "100", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
                out, error = ping.communicate()
                print out
                print error

if plat == "Linux":
        for line in lines:
                line = line.strip()
                ping = subprocess.Popen(
                        ["ping", "-c", "1", line],
                        stdout = subprocess.PIPE,
                        stderr = subprocess.PIPE
                )
                out, error = ping.communicate()
                print out
                print error

hostsFile.close()

Author:MHibbin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10634693/python-troubleshooting-ping-script
mhawke :

This code, as shown, will not work on Windows or Linux. You need to move ping.communicate() inside your loop, otherwise it will execute only once the loop has completed.\n\nYour for loop should read like this:\n\nfor line in lines:\n print line\n line = line.strip()\n ping = subprocess.Popen(\n [\"ping\", \"-c\", \"1\", \"-s\", \"1\", \"-l\", \"1\",line],\n stdout = subprocess.PIPE,\n stderr = subprocess.PIPE\n )\n out, error = ping.communicate()\n print out\n",
2012-05-17T11:39:31
Aaron Newton :

I believe this is what you're after:\n\nimport sys, os, platform, subprocess\n\nplat = platform.system()\nscriptDir = sys.path[0]\nhosts = os.path.join(scriptDir,'hosts.txt')\nhostsFile = open(hosts, \"r\")\nlines = hostsFile.readlines()\nprint lines\nif plat == \"Windows\":\n for line in lines:\n line = line.strip( )\n ping = subprocess.Popen(\n [\"ping\", \"-n\", \"1\", \"-l\", \"1\", \"-w\", \"100\", line],\n stdout = subprocess.PIPE,\n stderr = subprocess.PIPE\n )\n out, error = ping.communicate()\n print out\n print error\n\nelif plat == \"Linux\":\n for line in lines:\n print line\n line = line.strip()\n ping = subprocess.Popen(\n [\"ping\", \"-c\", \"1\", \"-s\", \"1\", \"-l\", \"1\",line],\n stdout = subprocess.PIPE,\n stderr = subprocess.PIPE\n )\n out, error = ping.communicate()\n print out\n print error\n\nhostsFile.close()\n\n\nNote the increased indents for the print statements, which should be in the for loops. I have just tried this on my machine and it is printing out a ping for each result in hosts.txt - I assume this is what you wanted?",
2012-05-17T11:42:27
yy