Send commands to an interactive shell from Python - python

Is there a way to send command to another interactive shell ? Let's take the example of the meterpreter shell used in metasploit. Could it be a way to say command to this shell from python code, as soon as I get control of a computer and have a meterpreter shell to play with ?
I mean All this from python code.

pexpect may be useful: http://pypi.python.org/pypi/pexpect/2.4

It will not be easy at all.
You will have to know if meterpreter has any means for other programs to communicate with it.
If it doesn't, you might want to go through hacking through it, e.g using OS pipes, etc to be able to get it to work.
In any case, the code needed for such communication might be beyond Python's power.

Related

How to embed shell in my Python curses application?

I am creating a TUI application using Python and curses. And I want to embed/integrate the shell, like you have in VS Code, etc. How can I do this?
I don't just want to capture input and execute it using bash, I want the real shell. E.g. in VS Code you see the real shell prompt (as defined in bashrc) and also you can run a whole application (e.g. vim) from inside the VS code terminal.
I have looked at pty module: not sure if it can help here. I want to basically have a library function like invoke_shell() to which I just pass a reference to a curses window (inside of which I want to show the shell). I do not need to interact with the shell in any other way, except to: (1) close it, (2) capture a keystroke for my application to move focus away from the shell window to another curses window in my application (and move focus back to it when I need it). Is there any such library?
Any help is greatly appreciated! Thanks in advance.

How to execute Scitools Understand commands from python

I want to execute Scitools Understand commands from python. It has its own shell which can be run using $ ./und and switches to und>. I want to run its commands from python. Any idea how can I do that?
First you should consider using the Python API for Ada Understand. This will be more comfortable than talking to the Understand console.
But using the console application is also possible. Ada Understand uses the standard input and output. Assuming you use Linux I have just a small demonstration for this.
create a file und.input with the contents
help
exit
And then exeucute this command
cat und.input | /opt/scitools/bin/linux64/und > und.output
This will create you the output into the file und.output.
You will not do it through files. With these files I just tried to give you evidence and a feeling that really the standard input and standard output is used. Knowing this, you need a way in Python to start a process and communicate to it over stdin and stdout. The subprocess module is a way to implement this. But there may be alternatives.
Although my demonstration above is on Linux, the Python implementation with subprocess will be the same for Windows.

Automating Subversion Commands using Python

I am pretty new to Subversion, and not that experienced in Python, but am doing some work with large volume of media-files that need moving around within the directory. Using the Visions GUI, some of the file transfers are taking a very long time, so I'd like to automate these tasks to run over night by storing the actions within a text file and then having a python script act on these overnight?
For example the text file might contain a command such as:
svn mv current desired
How can I send this string to Terminal to execute the command?
You could do os.system call or try using PySVN, which may give you more control in Python over SVN repository you're working with.
The subprocess module is the best way to execute commands. As #Abgan points out, the better way might be to use a subversion library instead.
If you're on Windows, it'd be better to use an SVN library. On Linux/Mac/Unix you could go either way realistically, because these can run a subprocess well - windows doesn't do terribly well at this.
subprocess is indeed preferred over os.system today.
The nice thing about using subprocess.Popen instead of an SVN library (module), is that you don't have to learn two ways of accessing SVN. Your command line SVN knowledge translates directly into your code.

[Python]How to launch a program using Thread

professionals
I know how to launch a command in Linux's terminal via process, sth likes following:
import subprocess
subprocess.Popen('ifconfig -a')
But this is opened in process, how can I launch that in a thread instead?
I know "thread.start_new_thread", while this should call a function. Within the function, I still have to use subprocess. And this just to open a process again..
Thank you for your help.
Respectfully..
A command like ifconfig always runs in a separate process. There is no way to run that command within only a "thread" of your application.
Perhaps you could provide more detail about why you believe this is necessary, and we may be able to suggest a different approach. For example, if you need to capture the output of the ifconfig command, there are certainly ways of doing that within Python.
As you are calling another process outside of your Python application, I think that there is no solution to make it run inside the Python interpreter.

Insert Command into Bash Shell

Is there any way to inject a command into a bash prompt in Linux? I am working on a command history app - like the Ctrl+R lookup but different. I am using python for this.
I will show a list of commands from history based on the user's search term - if the user presses enter, the app will execute the command and print the results. So far, so good.
If the user chooses a command and then press the right or left key, I want to insert the command into the prompt - so that the user can edit the command before executing it.
If you are on Linux, just fire up a bash console, press Ctrl+r, type cd(or something), and then press the right arrow key - the selected command will be shown at the prompt. This is the functionality I am looking for - but I want to know how to do that from within python.
You can do this, but only if the shell runs as a subprocess of your Python program; you can't feed content into the stdin of your parent process. (If you could, UNIX would have a host of related security issues when folks run processes with fewer privileges than the calling shell!)
If you're familiar with how Expect allows passthrough to interactive subprocesses (with specific key sequences from the user or strings received from the child process triggering matches and sending control back to your program), the same thing can be done from Python with pexpect. Alternately, as another post mentioned, the curses module provides full control over the drawing of terminal displays -- which you'll want if this history menu is happening within the window rather than in a graphical (X11/win32) pop-up.
See readline module. It implements all these features.
If I understand correctly, you would like history behaviour similar to that of bash in
a python app. If this is what you want the GNU Readline Library is the way to go.
There is a python wrapper GNU readline interface but it runs only on Unix.
readline.py is seem to be a version for Windows, but I never tried it.
ncurses with its python port is a way to go, IMHO.

Categories

Resources