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

cloudytechi147

New Member
Joined
Oct 12, 2021
Messages
4
Reaction score
0
Credits
63
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.

Code:
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]
print(output)

Windows Output:

Code:
Enter a host to ping: google.com

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

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

On Linux:

Code:
Enter a host to ping: google.com
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
 

Attachments

  • output.txt
    2.8 KB · Views: 306
  • pythonscript.zip
    634 bytes · Views: 364
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:
Python:
#!/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]
print(output)

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:

Staff online

Members online


Latest posts

Top