I need to give Gdb commands after I have started running a shell script which invokes gdb and halts to the Gdb prompt. So, to load and execute the image (.elf) file I invoke the following subprocess:
import subprocess
os.chdir(r"/project/neptune_psv/fw/")
print os.getcwd()
proc = subprocess.Popen('./Execute.sh -i TestList_new.in -m 135.20.230.160 -c mpu',shell = True,stdin = subprocess.PIPE)
After Execute.sh halts to the Gdb prompt I need to give two Gdb commands:
Set *0x44880810 = 3 (Set a register value)
Continue
Can anyone help me how to give these two commands through stdin ?
I think the simplest way is to use something like pexpect, an expect clone in Python. This provides a way to control an otherwise interactive process programmatically.
However, since you're specifically using gdb, note that this is not the best approach to driving gdb. There are two better ways. First, you can program gdb directly using its built-in Python API. This is the best way, if it is possible for your use case, as it is a lot simpler than trying to parse the output. However, if you must parse the output, then you should investigate "MI". This is gdb's "Machine Interface", which presents a vaguely JSON-like (it predates the real JSON) way to control gdb. There are MI parsing libraries available, though offhand I don't recall if there is one written in Python.
Related
I'm making my first steps in macOS app development.
I'm trying to write an app on Swift that would keep python interactive console open.
Sometimes I would like to send to python commands and return the results back to swift, but not closing python to keep all variables for the next command I will send.
Is there any way to do that?
As far as I understand, I can't use the Process() because the input pipe automatically closes when I run the task.
I probably need to use pseudo terminals with pty and tty, but I don't fully understand the idea and where to learn about it. (or, maybe, I'm wrong and there is another way)
If you actually want to use python code from swift, I would strongly advise you to avoid using this method. It is very bug prone and potentially limiting and inefficient. You better use some wrapper of the python-c-api, or write some small server in python to receive requests from swift.
If you still want to do that, an easy way to go about it would be to use python itself to spawn python inside a pty:
python -c "import pty, sys; pty.spawn(sys.argv[1:])" python
This will start a python console that reads and writes to stdio instead of of /dev/tty.
I know it might sound weird because I can do it using PowerShell, but I'd like to use Python to find what Windows Features are enabled.
I'll explain my reason and maybe you'll be able to guide me in a different direction, because from a quick google search, what I'm looking for is not possible.
I have a script that should be able to run on both Windows and Linux. It checks if a path to a directory exists, and if I have permissions to it. It does a bunch of other things and since it should work on both OSes, I need it to check that Windows Features thing. I don't want to run two different scripts, I'd rather have it all in one place and I'm not sure if maybe I'm able to call PowerShell inside my script.
Any idea what I can do?
I tried to search myself how to get the enabled windows features and found nothing, but i have some solution for you.
In my opinion you can run a powershell script inside your python script and get the result in a variable in you python script using subprocess module:
import subprocess
p = subprocess.Popen(["powershell.exe",
r"C:\Path\powershellscript.ps1"],
stdout=subprocess.PIPE)
output, err = p.communicate()
print(output)
The output variable will contain the data i believe you desire
So I've been using subprocess and pexpect
to try to interact with a separate program running in the terminal. I need to feed it a command, with arguments, and be able to receive it's response and potentially send it more commands.
With subprocess, I have only been able to launch a terminal, but not feed it commands. Or I can pass ONE line of command to an emulated terminal within python. The issue it that it's one-and-done and I can't really interact with it.
pexpect seems to only be able to initiate one command, and then respond to the terminal in an automated fashion, I couldn't find relevant and up to date documentation that went over what I needed.
Are there better modules to use for this? Or am I using them the wrong way?
-Thanks,
-Sean
pexpect is your best candidate, as far as I'm aware.
It's documentation matches version on pypi - 3.2 as for now.
If you would like to run bunch of commands one after another you can try to divide commands with ";" or "&", depends on your usage.
Btw. please take a look at example section.
What is the difference between subprocess.Popen() and os.system()?
If you check out the subprocess section of the Python docs, you'll notice there is an example of how to replace os.system() with subprocess.Popen():
sts = os.system("mycmd" + " myarg")
...does the same thing as...
sts = Popen("mycmd" + " myarg", shell=True).wait()
The "improved" code looks more complicated, but it's better because once you know subprocess.Popen(), you don't need anything else. subprocess.Popen() replaces several other tools (os.system() is just one of those) that were scattered throughout three other Python modules.
If it helps, think of subprocess.Popen() as a very flexible os.system().
subprocess.Popen() is strict super-set of os.system().
os.system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed
Whereas:
The popen() function opens a process by creating a pipe, forking, and
invoking the shell.
If you are thinking which one to use, then use subprocess definitely because you have all the facilities for execution, plus additional control over the process.
Subprocess is based on popen2, and as such has a number of advantages - there's a full list in the PEP here, but some are:
using pipe in the shell
better newline support
better handling of exceptions
When running python (cpython) on windows the <built-in function system> os.system will execute under the curtains _wsystem while if you're using a non-windows os, it'll use system.
On contrary, Popen should use CreateProcess on windows and _posixsubprocess.fork_exec in posix-based operating-systems.
That said, an important piece of advice comes from os.system docs, which says:
The subprocess module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is
preferable to using this function. See the Replacing Older Functions
with the subprocess Module section in the subprocess documentation for
some helpful recipes.
I was wondering if it was possible to write a GUI in python, and then somewhere in the python script, insert a script switch to temporarily change the language to accomodate for the batch snippet.
I know this can be done in html and vbscript but what about Python?
You can control other processes, written with any language, including bash using the subprocess module.
The subprocess module is the most powerful and complete method for executing other processes. However, there's also a very simple method using the os module: os.system(command) runs command just as if you were to type it into a command line.