Reading a subprocesses stdout on Windows

B

brolewis

I have been trying for several weeks now to write a program that allows
me to read the stdout of a process that I spawn and once I receive
feedback, act appropriately.

More specifically, I need to create an SSH tunnel using plink on
Windows XP. Once the tunnel is successfully created, then I need to run
my program over that tunnel. If there is an error in the creation of
the tunnel, then I need to abort and warn the users.

My needs are 'small' but I just lack the know-how to get it done. I
need to be able to spawn off plink so that it can continue to run in
the background without interferring with the flow of my program.
However, I would like to get the feedback to know if/when a successful
connection has been made so that I know the appropriate actions to
take.

In the past, I spawned off a process using os.spawnl set into a thread
and would pause for 10 seconds to give plink time to connect. However,
it probably goes without saying this is not the best solution by any
stretch of the imagination.

My old command was (with obviously fake data):
os.spawnl(os.P_NOWAIT, 'plink', 'plink', '(e-mail address removed)', '-N',
'-L 111:200.200.200.200:7000')
Then I would poll Windows to see if plink had started. If it hadn't
started, I would raise an exception. However, there are plenty of times
where plink starts but no connection is made.

As with most questions like this one, any and all help would be greatly
appreciated.

Lewis
 
K

Klaus Alexander Seistrup

BroLewis said:
I have been trying for several weeks now to write a program that
allows me to read the stdout of a process that I spawn and once
I receive feedback, act appropriately.

Have you looked into the 'commands' module?

Cheers,
 
D

Do Re Mi chel La Si Do

Hi!

Let down subprocess, and remember popen4.

Here, an example (with CMD, under w-XP) :

import os

def lcmd(lst=None):
a = os.popen4(lst[0])
for i in lst[1:]:
if i!='':
a[0].write(i+'\r\n')
a[0].flush()
return a[1].readlines()

l=[
'CMD /K',
'DIR *.c /B',
'DATE /T',
'TIME /T',
'EXIT'
]
lret = lcmd(l)
for n in lret:
print n[:-1]



Attention : don't read data inside the loop (for)...



@-salutations

Michel Claveau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top