How can I prevent infinite loop in subprocess of python? - python-2.7

I am writing a python script that executes another program with bash command.
I am using subprocess.Popen() function for it. However in some conditions the external program gives me an error and tries to run same comment infinitely.
How can I catch that error and break Popen function.
Assume that program gives its error as an output.

You can use poll() or communicate() function for this. These methods return returncode once the process terminates. The return code is None as long as programme is running.
You can try using below code
from subprocess import Popen
process = Popen('bash your_script.sh')
while process.poll() is None:
print 'keep running'
process.terminate()

Related

inject code onto command line input using python27

I have written a simple c program with a bufferoveflow. It is basically a game to guess 4 digits number but starts by asking players to enter their name and this is where buffer overflow happens...I have written an exploit to basically inject shellcode when the "Please enter your name" When I run it without program attached to the immunity debugger it works fine but when I attach the exe file to the immunity debugger python script does noting as it is not something that is running on the debugger.....so basically nothing happens when I execute the code. Python code is below:
import sys, struct, os
import subprocess
import time
from subprocess import Popen, PIPE
location ='C:\Users\ZEIT8042\Desktop\Guessthenumber\guess.exe
p= Popen([location],stdin=PIPE,stdout=PIPE,stderr=PIPE)
time.sleep(15) #tried this to make the program stall for 15 seconds so that it can be attached to immunity debugger.
junk='A'*40
o,e= p.communicate(input=junk)
print(o)
What I am trying to do is check if the program is running...if it is running then inject the shellcode when the exe asks for the name.....any help would be appreciated...
elif is used in multiple conditions that is seen wrong.is this wrong meaning

Trying to get some output from subprocess.Popen works for all commands but bzip2

I am trying to get the output from subprocess.Popen assign it to a variable and then work with it in the rest of my program however it is executing code without ever assigning it to my variable
my current line of code is
result = subprocess.Popen('bzip2 --version', shell=True, stdout=subprocess.PIPE).communicate()[0]
currently to test it im just printing the length and the results which are currently empty
it does execute the code but it shows up in the terminal prior to my prints
I have tried the above-mentioned code using other commands and it works just as I would expect
any suggestions on how I can go about doing this?
Seems bzip2 writes to stderr instead of to stdout.
result = subprocess.Popen('bzip2 --version', shell=True, stderr=subprocess.PIPE).communicate()[1]

How to quit python script in gdb-python?

I am writing a python(v2.7) script in GDB(v7.5.1-0.7.29). I want to quit the python script when certain condition got false. But i do not want to quit GDB. I tried using sys.exit(), exit() and quit(), but in those case they also quit GDB. Is there any way to just quit the python script but not the gdb. Like ctrl + c command but i want this happend only when a certain condition got false.
CTRL+D does that
(gdb) pi
>>> [Press CTRL+D]
(gdb)
You have to raise an exception. Question is what exception to raise; whether you provide code to catch it and whether you want traceback to be printed or not.
If you don't mind traceback to be printed you can raise KeyboardInterrupt. But I presume you want graceful exit without traceback.
If you are writing code for class that integrates into GDB command then you can raise gdb.GdbError. It will be consumed by the code implementing command class and no traceback is printed.
If you are writing a script that is executed by sourcing it then you have to embed your whole script into try / except and catch exception you are raising. In fact, calling exit() also simply raises exception SystemExit, so you may write your script as:
try:
some code
if some_condition:
exit()
some more code
except SystemExit:
pass

Restart Python script if finish or crash Windows

Now I have a infinite loop in my code for the script restart when finish. The problem is when script crash.
How I can restart script if crash?
infiniteloop.py
while True:
execfile("abc.py")
abc.py
print "1"
error
Do like this,
while True:
try:
execfile("abc.py")
except Exception as e:
print "Exception occured: ", e
There's probably not any way to have the script restart itself once it's crashed, but you could add some error handling like so:
while True:
try:
execfile("abc.py")
except:
pass
Alternatively, you could use shell scripting to run your wrapper repeatedly.

popen Hangs and cause CPU usage to 100

I have a code that use popen to execute a script ,It works fine but randomly it's blocking and getting CPU to 100% ,after a little investigation I discover that it hangs on popen calls. I have put a printf after the popen showing the descriptor asigned and in this case when it blocks this printf never shows.
What can cause popen to block?
Edit: Code
FILE* pipe = popen(cpCommand, "r");
printf(....
if (pipe)
{
while (!feof(pipe))
{
if (DataReady(fileno(pipe),2500)>0)
{
if (fgets(output,sizeof(output),pipe) != NULL)
{
DataReady is just a select..
I have done a strace after it blocks and it seems to not doing anything
Not an answer ;-)
Try use strace to what it's doing and which syscall hangs.
Tterminal output is line-buffered, so make sure to flush output by using fflush() or using a newline (fflush(stdout); or printf("Debug text\n");) to ensure it doesn't really call the printf().
Run the script manually and ensure the script doesn't hang intermittently before suspecting popen().
Check the script called by popen() why it doesn't end.
As long a s the script does not end, popen() blocks, will say: does not return, as observed.
I strongly doubt this is a C issue.