Parsing stdout through a pipe?

Discussion in 'Python' started by MK, Jul 12, 2003.

  1. MK

    MK Guest

    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?
     
    MK, Jul 12, 2003
    #1
    1. Advertising

  2. MK

    Noah Guest

    "MK" <> wrote in message news:<bepf5g$7u6st$-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
     
    Noah, Jul 13, 2003
    #2
    1. Advertising

  3. MK

    Miki Tebeka Guest

    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
     
    Miki Tebeka, Jul 13, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    490
    Jack Klein
    Nov 2, 2004
  2. Elad
    Replies:
    0
    Views:
    412
  3. Replies:
    11
    Views:
    547
    akarl
    Aug 13, 2005
  4. Replies:
    10
    Views:
    774
    SM Ryan
    Aug 12, 2005
  5. Replies:
    1
    Views:
    226
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page