Python sub process script works on my Windows but not on my Linux


New Member
Oct 12, 2021
Reaction score
I write a simple script to check the subprocess module and I tested it on both Windows and Linux.

The script works fine on Windows but not on Linux.

The interpreter in python is used in 3 versions on both.

import subprocess
host = input("Enter a host to ping: ")
p1 = subprocess.Popen(["ping", "-n", "2", host], shell=True, universal_newlines=True, stdout=subprocess.PIPE)
output = p1.communicate()[0]

Windows Output:

Enter a host to ping:

Pinging [] with 32 bytes of data:
Reply from bytes=32 time=17ms TTL=120
Reply from bytes=32 time=16ms TTL=120

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

On Linux:

Enter a host to ping:
ping: usage error: Destination address required

Let me know the difference when running on Linux?.

That's odd. Based on the ping error message, it isn't providing a host to the ping command. I would add print(host) between the input() and subprocess.Popen() calls and validate it does have the host name you typed in.

Something might be gumming up your input() call or at least the host variable it is saving it too.
attached is output of a python script and python script that I just ran on my Arch Linux

i've had to zip the python since .py not allowed. SO linux in itself not an issue . My version is:

[andrew@darkstar:~/Desktop]$ python --version (11-01 17:11)
Python 3.9.7

Are you getting into python ? i've done a nice python with deps->elf for Arch Aur if you want to have a look


  • output.txt
    2.8 KB · Views: 346
    634 bytes · Views: 404
The reason it's not working in Linux is because of the following:
In the GNU implementation of ping used in Linux, the -n option is the short option for --numeric - which does not resolve host addresses. So it would expect the host as a numeric IP. Also the parameter immediately after the -n is a single digit 2, which is not a valid host IP. So you'll see the ping: unknown host error message.

In the GNU version of ping - the -c or --count option is used to determine the number of times to ping. So in Linux, you need to use the -c option, not the -n option. Don't blame Linux though! Blame M$ for their non-standard implementation of ping! Ha ha!

So, if your script is going to be ran on Windows and Linux, perhaps consider setting your script up like this:
#!/usr/bin/env python3
import platform
import subprocess

host = input("Enter a host to ping: ")
command="ping {countParam} 2 {hostName}".format(countParam='-n' if platform.system().lower()=='windows' else '-c', hostName=host)
p1 = subprocess.Popen(command, shell=True, universal_newlines=True, stdout=subprocess.PIPE)
output = p1.communicate()[0]

In the above code, we built the command we're going to run in a separate variable, using Python's String class's format() method.
We added two place-holders in the command string. One for the platform-dependent count parameter {countParam} and one for the host name {hostName}.
And in the format method, we substitute-in values for the two placeholders.
For {countParam} placeholder - we use the platform module to determine whether or not we're on windows. If we are on windows, the {countParam} placeholder is replaced with -n, otherwise it's replaced with -c.
The syntax used in that assignment might look a bit odd, but trust me - it's valid python and it works. It was the only way I could succinctly get that logic into a single-line assignment! Ha ha!

And for the second placeholder {hostName}, we simply substitute the host-name that the user had previously entered into the host variable.

So those substitutions will insert the appropriate count parameter and host into the command.
So now your script should run properly on Linux and Windows!

I hope this all makes sense and helps!
Last edited:

Members online