Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7963265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:34:20+00:00 2026-06-04T05:34:20+00:00

I have hit another wall… I have the following script, which appears to work

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:34:22+00:00Added an answer on June 4, 2026 at 5:34 am

    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.

    Your for loop should read like this:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have hit another wall. After getting my key input working, I have been
I'm onto problem 245 now but have hit some problems. I've done some work
I'm trying to do a cross domain POST request and have hit a wall
My app has been running fine on iPhone simulator. I have hit an issue
Hit the following situtation a couple of times today: Myself and another dev both
i've hit another brick wall today with jquery generated partialviews when located within areas.
I have hit a brick wall on this one, but I would like someone
I've been debugging the following issue for quite awhile now and have hit a
I have hit upon this problem about whether to use bignums in my language
I have hit a classic problem of needing to do a string replace on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.