Python making cmd invisible when using the os.system - python

I found out that i could ping a system on python by typing
os.system('ping ip')
but when i execute it, it shows cmd.
My question is, how do i ping someone on python without showing the cmd?

If you need only a ping, then it would be better to use something like ping.py.
In other cases use subprocess as suggested by #Sentinel

Look at the
http://docs.python.org/library/subprocess.html
module.
It gives you enough options for controlling the output.
Or use standard bash redirection in order to send the output to /dev/null

See my answer to hiding console when run in os.startfile()

Related

How to use python to write a linux shell command?

I want to use python to write a shell command... that I can customize some paths in my config file, that I can "shortcut" to these paths directly, not need to "cd" and "cd" again and again...
I want to use python, because I don't know anything about bash. Is that possible ?if yes, could you give me any idea about how to use python to fullfill this shell command...
Thanks !
You have to give a look to the os library of python https://docs.python.org/2/library/os.html
The os.path module helps you to deal with paths. https://docs.python.org/2/library/os.path.html#module-os.path
With the subprocess module you can also execute commands, configure timouts (if the command hangs out), check the result, etc https://docs.python.org/2/library/subprocess.html

How to give multiple GDB commands through subprocess?

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.

Using python to talk to a terminal program back/forth

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.

"script command" and logging in python

I have a python app that has lots of outputs on the screen which can be used for debugging. out of all the logging techniques, "script" command works well for me because I can see the output on the screen as well as logging it. I want to include that at the beginning of my python app to run automatically and log everything, when I do, however, the python program doesn't run. as soon as I type exit at the terminal (which stops script logging) the app starts working. The command I'm using is:
command="script /tmp/appdebug/debug.txt"
os.system(command)
I have also tried script -q but the same issue is there. Would appreciate any help.
Cheers
Well, I did find the answer for anyone who is interested:
https://stackoverflow.com/questions/15507602/logging-all-bash-in-and-out-with-script-command
and
Bash script: Using "script" command from a bash script for logging a session
I will keep this question as others might have the same issue and finding those answers wasn't exactly easy :)
Cheers
Try to use subprocess, like so:
from subprocess import Popen, PIPE
p = Popen(['script', '/tmp/appdebug/debug.txt'], stderr=PIPE, stdout=PIPE)
stdout, stderr = p.communicate()
script is a wrapper for a session of interactions. Even if it appears to terminate quickly after being started in a shell, this is not so; instead it starts a new shell in which you can interact so that everything is logged to a file.
What does this mean for you?
Your approach of using script cannot work. You start script using os.system which will wait for script to terminate before the next Python statement is executed. script's work will only happen before it terminates (i. e. during the uninteresting waiting period of your Python program).
I propose to use script -c yourprog.py yourprog.log instead. This will execute and wrap the yourprog.py and the session will be stored in yourprog.log.

python open applications through your program

I want my program to open iTunes during runtime. How do I implement this ?
I looked around for answers but didn't get any concrete complete answers. Till now, all I know is I could use the os module and then call the os.system() function to open iTunes. If this is right, what goes into the brackets ?
I have a Mac OS X machine.
One straightforward way to do this on Mac OS X will be to use the open command:
os.system("open -a iTunes")
There are undoubtedly other ways of doing this (e.g, using the Cocoa/Python bridge), but this is the simplest.
This should be able to help you.
From the article linked above...
import sys, string, os, arcgisscripting
os.chdir( 'c:\\documents and settings\\flow_model' )
os.system( '"C:\\Documents and Settings\\flow_model\\flow.exe"' )
Use subprocess.call() if you want to simply run an executable.
os.system() run the command in a subshell, which generates an unnecessary extra process and slightly different behavior depending on the operating system/console used (for example cmd.exe have different escaping than bash)
Read subprocess, is better than os.system in your case.
Subprocess module: http://docs.python.org/2/library/subprocess.html

Categories