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

cloudytechi147

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


dcbrown73

Well-Known Member
Joined
Jul 14, 2021
Messages
361
Reaction score
332
Credits
3,199
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.
 

captain-sensible

Well-Known Member
Joined
Jun 14, 2019
Messages
2,609
Reaction score
1,756
Credits
15,708
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:

[[email protected]:~/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: 46
  • pythonscript.zip
    634 bytes · Views: 41

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,374
Reaction score
1,946
Credits
9,075
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:
$100 Digital Ocean Credit
Get a free VM to test out Linux!


Top