Parsing stdout through a pipe?

M

MK

I have a Win32 console application (SNMPUTIL.EXE) which listens
to incoming SNMP messages:

C:\>snmputil trap
snmputil: listening for traps...


When a trap is generated on a remote server, it is being sent to
the PC running SNMPUTIL.EXE, and then finally printed out to
stdout like this:

snmputil: trap generic=6 specific=11003
from -> 10.198.163.89
Variable = system.sysName.0
Value = String DEAUDIIP109387
Variable = .iso.org.dod.internet.private.enterprises.232.11.2.11.1.0
Value = Integer32 0
Variable = .iso.org.dod.internet.private.enterprises.232.11.2.8.1.0
Value = String Compaq Management Agents Test Trap sent - Samstag, 12.
Juli 2003 18:52:19


I'd like to write a Python application which would intercept/parse this
stdout
output, and invoke various pop-ups ("alarms"). Any ideas?
 
N

Noah

MK said:
I have a Win32 console application (SNMPUTIL.EXE) which listens
to incoming SNMP messages:

C:\>snmputil trap
snmputil: listening for traps...
...
I'd like to write a Python application which would intercept/parse this
stdout
output, and invoke various pop-ups ("alarms"). Any ideas?

You can use popen and friends to create a pipe to listen to the output
of an external application, but there are many pitfalls to using a
simple pipe to control an external application.

Do you need to respond to the output by writing message back through stdin?
If yes, then pipes are a bad way to go. Does snmptrap print events continuously
(versus printing just a single trap even and then exiting)?
If it runs continuously then a pipe is a bad way to go.
The reason these scenarios are bad has to do with way the stdio buffers pipes.
Stdio will not necessarily flush the output buffer after snmptrap prints
some data. Stdio will flush the buffer when full, not after data is printed.
So your snmptrap may print some data, but it will just sit in the buffer
until snmptrap prints more data to fill it the buffer. There is no way for you
to force the buffer to flush.

The way around this is to use UNIX where you can use a pseudo TTY :)
You can also use Cygwin... I am not sure if there is an ideal solution
under Windows. Cygwin manages to implement a pseduo TTY system on Windows, so
there must be some sort of solution using the win32 API.

I have a pseudo TTY wrapper library called pexpect here:
http://pexpect.sourceforge.net/
It works pretty well under Cygwin Python, but not under regular Windows python.

On the other hand if your snmptrap program just prints one event and then
exits then you can use pipes because the buffer will be flushed when
snmptrap exits. Or, if you can modify the source to snmptrap then you can
have it force a flush on stdout after each print.

Yours,
Noah
 
M

Miki Tebeka

Hello MK,
C:\>snmputil trap
snmputil: listening for traps...


When a trap is generated on a remote server, it is being sent to
the PC running SNMPUTIL.EXE, and then finally printed out to
stdout like this:

snmputil: trap generic=6 specific=11003
from -> 10.198.163.89
Variable = system.sysName.0
Value = String DEAUDIIP109387
Variable = .iso.org.dod.internet.private.enterprises.232.11.2.11.1.0
Value = Integer32 0
Variable = .iso.org.dod.internet.private.enterprises.232.11.2.8.1.0
Value = String Compaq Management Agents Test Trap sent - Samstag, 12.
Juli 2003 18:52:19


I'd like to write a Python application which would intercept/parse this
stdout
output, and invoke various pop-ups ("alarms"). Any ideas?
Have a look at the popen2 module.
I'd use wxPython or TKInter for the PopUps.

HTH.
Miki
 

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