0

I'm trying to use Go binary as well some shell packages in a python script. It's a chain command using |, for summary the command would look like this:

address = "http://line.me"
commando = f"echo {address} | /root/go/bin/crawler | grep -E --line-buffered '^200'"

Above code is just a demonstration, where the actual code is reading address from a wordlist. First try using os.system, it fails.

read = os.system(commando)
print(read)

Turns out os.system doesnt transfer any std. I had to use subprocess:

commando=subprocess.Popen(commando,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
            commandos = commando.stdout.read() + commando.stderr.read()  
            print(commandos)

Mentioning shell=True triggers:

b'/bin/sh: 1: Syntax error: "|" unexpected\n'

Trough more reading, it could be because sh can't read | or i need to use bash. Is there any alternative to this? have been trying to use shebang line in commando variable:

#!/bin/bash

Still doesn't push my luck...

  • Try escaping the pipe character with backslash. `\|` – zooid Aug 03 '22 at 11:02
  • some tips here https://stackoverflow.com/a/16709666/202168 and https://stackoverflow.com/questions/13332268/how-to-use-subprocess-command-with-pipes and https://docs.python.org/3.9/library/subprocess.html#replacing-shell-pipeline – Anentropic Aug 03 '22 at 11:10
  • I can't reproduce this problem. That command line seems perfectly valid. – larsks Aug 03 '22 at 11:36

2 Answers2

1

Try this way:

subprocess.call(commando, shell=True)

the following code worked for me!

subprocess.call('ls | grep x | grep y', shell=True)
0

Fixed by explicitly mentioning bash:

['bash', '-c', commando]
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 06 '22 at 03:25