0

I am trying to make a simple ping (ping target.com) in a terminal.

The problem is the terminal counts the quotation marks in the string "ping". The result of my code now will be:

""ping target.com"" and this will run a error --> ""ping target.com"" is not recognized as an internal or external command

I know that problem is the double mark but I don´t know how to remove the quotation mark from the ping.

Here the code:

target = input('[+] Please put the IP target: ')


ping = subprocess.Popen(["ping " + target], shell=True, stdout=subprocess.PIPE)
out = ping.communicate()[0] #access stdoutdata
print(out)
Drakarys
  • 41
  • 5
  • [how to user subprocess.Popen](https://stackoverflow.com/questions/12605498/how-to-use-subprocess-popen-python) , use `ping = subprocess.Popen(["ping ", target], shell=True, stdout=subprocess.PIPE)` – sahasrara62 Apr 01 '21 at 23:55
  • Hi @ sahasrara62 I tried it but it still doesn't work – Drakarys Apr 02 '21 at 00:02
  • 1
    [passing-variables-to-subprocess-popen](https://stackoverflow.com/questions/20140137/passing-variables-to-subprocess-popen) use `ping = subprocess.Popen(f"ping {target}",shell=True, stdout=subprocess.PIPE)` if you want to use `shell=true` else use `ping = subprocess.Popen(["ping", target], stdout=subprocess.PIPE)` – sahasrara62 Apr 02 '21 at 00:09
  • @sahasrara62 Thank you dude! Is working! =) – Drakarys Apr 02 '21 at 13:47

2 Answers2

0

Try ["ping", target]

Bobby Durrett
  • 1,223
  • 12
  • 19
0

Two issues. When passing a command to subprocess.Popen, the documentation says this for ushing shell=True:

The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

Also, you're printing the output of the communicate call, which is a byte array. You probably want to decode it, so it's looks correct to the user:

import subprocess
target = input('[+] Please put the IP target: ')
# Just pass a normal string to Popen
ping = subprocess.Popen("ping " + target, shell=True, stdout=subprocess.PIPE)
out = ping.communicate()[0] #access stdoutdata
# Decode the output to a normal string
print(out.decode("utf-8"))
Anon Coward
  • 9,784
  • 3
  • 26
  • 37