Python Popen - how to execute commands in nested sub shell using python - python

"I have an issue executing commands in nested adb sub shell in python. executing "command_two" in adb shell opens a sub console in command line (and the console waits for input). how do i execute commands (give input to the console) in that console using the python.
Code:
R = subprocess.Popen('adb shell', stdin=subprocess.PIPE)
R.communicate('command_one\ncommand_two\n)

Please try this:
import time
R = subprocess.Popen('adb shell', shell=True, stdin=subprocess.PIPE)
R.communicate('command_one\n')
time.sleep(2)
R.communicate('command_two\n')

Related

Python - subprocess with creationflags (0x00000008) - How to get output after process ends? (Windows)

I wrote a Python script to run a terminal command that belongs to a 3rd party program.
import subprocess
DETACHED_PROCESS = 0x00000008
command = 'my cmd command'
process = subprocess.Popen(
args=command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
creationflags=DETACHED_PROCESS
)
code = process.wait()
print(process.stdout.readlines())
# Output: []
This script basically runs the command successfully. However, I'd like to print the output but process.stdout.readlines() prints an empty list.
I need to run the subprocess with creationflags due to 3rd party program's terminal command.
I've also tried creationflags=subprocess.CREATE_NEW_CONSOLE. It works but process takes too long because of 3rd party program's terminal command.
Is there a way to print the output of subprocess by using creationflags=0x00000008 ?
By the way, I can use subprocess.run etc to run the command also but I'm wondering if I can fix this.
Thank you for your time!
Edit:
I'm sorry I forgot to say I can get output if i write "dir" etc. as a command. However, I can't get any output when I write a command such as: command = '"program.exe" test'
I'm not sure that this works for your specific case, but I use subprocess.check_output when I need to capture subprocess output.
import subprocess
DETACHED_PROCESS = 0x00000008
command = 'command'
process = subprocess.check_output(
args=command,
shell=True,
stderr=subprocess.STDOUT,
encoding="utf-8",
creationflags=DETACHED_PROCESS
)
print(process)
This just returns a string of stdout.

How to run python subprocess with real-time output?

When I run a shell command in Python it does not show the output until the command is finished. the script that I run takes a few hours to finish and I'd like to see the progress while it is running.
How can I have python run it and show the outputs in real-time?
Use the following function to run your code. In this example, I want to run an R script with two arguments. You can replace cmd with any other shell command.
from subprocess import Popen, PIPE
def run(command):
process = Popen(command, stdout=PIPE, shell=True)
while True:
line = process.stdout.readline().rstrip()
if not line:
break
print(line)
cmd = f"Rscript {script_path} {arg1} {arg2}"
run(cmd)

How i can interact with some CLI runned in terminal?

For now i have some Command-Line Interface which one i need to interact. (e.g. run this CLI in terminal, then execute few comands inside of it).
In terminal it looks the next way:
blabla-cli
some-comand
%response%
next-command
And all of it i want to automate with python.
Almost all what i could fing in the Google is about using Popen and comunicate or process.stdin to do what i want.
But in case of using provided code i can't send any comand to process, i mean "help" wont be sended to terminal
args = ["blabla-cli"]
process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
process.wait()
process.stdin.writelines('help')
process.wait()
print(process.stdout.readlines())
I expect to send some command to terminal via python code, get some response and get some actions with recieved response.

Output from two processes on shell (Python,Linux)

My python script executes program that sends its output on shell.
However, script should simultaneously execute its own commands which also have its output on shell.
Is it possible to do that? Will I see output both from the script and from the process?
Here is the process that has output on shell:
s.exe_cmd('./upgrade')
So, will I be able to write
print "my output..."
and see it on shell to?
Assuming you're using subprocess - it will print both on the console.
import subprocess
p = subprocess.Popen("sleep 1 && echo from inside subprocess",
shell=True)
print("will print asynchronously")
p.wait()
This is an example program that you can try out

Opening another command line interpreter and typing commands

I use "spim" emulator to emulate the mips architecture. The way it works is that I should first have a "filename.asm" file, I then type "spim" in bash to open the command line interpreter for spim, then I can use the spim commands like loading the file and running it, etc..
I am trying to write a python script that opens the spim command line interpreter and starts typing spim commands in it. Is this possible?
Thanks.
This is going to depend on spim, which I'm not familiar with, but if you can pipe something to it, you can do the same in Python
Check out http://docs.python.org/library/subprocess.html
Something like this will get you started:
proc = subprocess.Popen('spim',shell = True,stdin = subprocess.PIPE)
proc.stdin.write("Hello world")
from subprocess import Popen, PIPE, STDOUT
# Open Pipe to communicate with spim process.
p = Popen(['spim'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True)
# Write a "step 1" command to spim.
p.stdin.write('step 1\n')
p.stdin.close()
# Get the spim process output.
spim_stdout = p.stdout.read()
print(spim_stdout)

Categories