473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Parsing stdout through a pipe?

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?
Jul 18 '05 #1
2 7326
"MK" <MK@foo.com> wrote in message news:<be************@ID-174077.news.uni-berlin.de>...
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
Jul 18 '05 #2
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
7
by: Edward Diener | last post by:
From within a function in my own module I need to take the output from a Python module "A", which is sending data to stdout and which can not be changed and which I need to invoke as a top-level...
2
by: Graham Ashton | last post by:
Hi. I'm having trouble flushing sys.stdout. I've written a small example to illustrate my problem (see below). In short, I expect it to ping "received hello", sleep for 5 seconds and then print...
2
by: Leon | last post by:
import os,sys a = os.fdopen(sys.stdout.fileno(),"w") b = os.fdopen(sys.stdout.fileno(),"w") a.write("test1 ") b.write("test2 ") stdout result is test1 test2
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
0
by: kreismaler | last post by:
I have some problems to understand the difference of using the STDOUT and using "anonymous pipes" as shown below: using System; using System.Diagnostics; using System.IO; namespace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.