How to execute Scitools Understand commands from python - 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.

Related

Python module for shell commands

I've got a bash script that's starting to cross that grey area where it should probably be rewritten in a scripting language. Since I'm constantly tweaking the script bash is perfect because of how concise things are. Mostly I'm manipulating output from one program to put into another program (i.e. using cat, grep, sed, tail, head). I'm familiar with Python so I'm looking for a module that can essentially perform these commands. I've found some stuff that uses subprocess to call bash commands but I don't want a wrapper for bash commands. I also realize I could simply take the time to write most of these commands in python and even chain them fairly easily if I encapsulated them all into one class. It seems like such an obvious thing though I find it hard to believe these utilities don't exist in a module that already exists.
You can use the os module in python to have a command executed exactly as it would be in a command shell.
For example, if you have a file named "junk.txt" and want to use the "head" command on it, you would write head junk.txt in your shell.
In Python, it's
import os
os.system("head junk.txt")

How did I get informations on the module I'm writing?

I'm writing my first complete python project with Vim. As I was modifying a
file I accidentally hit several keys that I can't find back and I get this
prompt:
I didn't know it was possible to get this kind of help on a module I am
writing and I have no idea how I got it, so my question is:
What command or tools allows to generate this kind on module information?
Several notes
The command is not a Vim command because the ouput was in an external
shell (so I probably use an equivalent to :![command].
I don't have any Vim plugin related to python installed so it was probably not generated by a plugin.
The command wasn't issued in an interactive python prompt since I started my vim from my bash prompt.
I have not idea of how many keystrokes I used.
My Vim command history and my bash history doesn't have a trace of what
happened.
I'm using zsh and oh-my-zshell
I know that this question might sound silly but I have no idea of which tool can do that and I have no mean to find what sequence of keystrokes I used.
You can use pydoc command to get module help
pydoc requests
if you are using the interactive python shell, you can use the help function:
>>> import requests
>>> help(requests.get)
it work on class instance too

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.

Send commands to an interactive shell from 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.

python programming query

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.
After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.
If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

Categories

Resources