Python script clean-up
I have a simple script that will take a list of hosts and ping each host (theres about 200) a single time before moving on. This is not the most effecient method I am sure, as it is very linear. And it takes a few minutes to complete. I would ideally like to run this script every minute (so each IP address is checked every minute (the actual running of the script is controlled externally).

I was looking for some pointers on potentially making the script more effecient/dynamic.

Here is my script...

    import sys
    import os
    import platform
    import 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", "-l", "1", "-s", "1", "-W", "1", line],
    			stdout = subprocess.PIPE,
    			stderr = subprocess.PIPE
    		)
    		out, error = ping.communicate()
    		print out
    		print error
    
    hostsFile.close()

Any thoughts would be appreciated.

Thanks,

MHibbin
Python script clean-up
===

I have a simple script that will take a list of hosts and ping each host (theres about 200) a single time before moving on. This is not the most effecient method I am sure, as it is very linear. And it takes a few minutes to complete. I would ideally like to run this script every minute (so each IP address is checked every minute (the actual running of the script is controlled externally).

I was looking for some pointers on potentially making the script more effecient/dynamic.

Here is my script...

    import sys
    import os
    import platform
    import 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", "-l", "1", "-s", "1", "-W", "1", line],
    			stdout = subprocess.PIPE,
    			stderr = subprocess.PIPE
    		)
    		out, error = ping.communicate()
    		print out
    		print error
    
    hostsFile.close()

Any thoughts would be appreciated.

Thanks,

MHibbin