Redirecting stdin/stdout to self

Discussion in 'Python' started by Jan Danielsson, Jan 23, 2006.

  1. Hello, I thought I'd write a program to collect information from pf
    (packet filter) and insert it into a postgresql database for review on a
    web page. First I checked if this has been done already, and found that
    it has.. Using Perl and SQLite in a program called "hatchet".

    Well, I want to do it in Python, and I want to use postgresql.

    Anyway, upon looking in the hatchet Perl code I found this:

    open(IN, "$tcpdump -neltttr $log 2>&1 |");

    That looks kind of funky... And if I'm reading it correctly, the Perl
    script's process starts tcpdump, but redirects its output to its own
    input, and reads it line by line.

    I would normally have done it like this:

    $ tcpdump -nelttt pflog0 | mypythonscript.py

    ...however, the Perl script solution looks interresting.. Is it
    possible to do something like that in Python?

    --
    Kind Regards,
    Jan Danielsson
    Te audire non possum. Musa sapientum fixa est in aure.
     
    Jan Danielsson, Jan 23, 2006
    #1
    1. Advertising

  2. On 2006-01-23, Jan Danielsson <> wrote:

    > And if I'm reading it correctly, the Perl
    > script's process starts tcpdump, but redirects its output to its own
    > input, and reads it line by line.

    [...]
    > ...however, the Perl script solution looks interresting.. Is it
    > possible to do something like that in Python?


    os.popen()

    http://www.python.org/doc/current/lib/os-process.html


    --
    Grant Edwards grante Yow! Yow! I threw up on
    at my window!
    visi.com
     
    Grant Edwards, Jan 23, 2006
    #2
    1. Advertising

  3. On 2006-01-23, Grant Edwards <> wrote:
    > On 2006-01-23, Jan Danielsson <> wrote:
    >
    >> And if I'm reading it correctly, the Perl
    >> script's process starts tcpdump, but redirects its output to its own
    >> input, and reads it line by line.

    > [...]
    >> ...however, the Perl script solution looks interresting.. Is it
    >> possible to do something like that in Python?

    >
    > os.popen()
    >
    > http://www.python.org/doc/current/lib/os-process.html


    I should have also added that there's a module that allows you
    to call libpcap directly (libpcap is the library that tcpdump
    uses to capture packets).

    http://sourceforge.net/projects/pylibpcap/

    It's way, way more efficient than parsing tcpdump's output. If
    you're only grabbing a few packets it may not matter. For some
    of the apps I've done, using pylibpcap has cut run-times by a
    factor of 10 or more.

    --
    Grant Edwards grante Yow! Life is selling
    at REVOLUTIONARY HAIR
    visi.com PRODUCTS!
     
    Grant Edwards, Jan 23, 2006
    #3
  4. Jan Danielsson

    Dan M Guest

    >>> And if I'm reading it correctly, the Perl
    >>> script's process starts tcpdump, but redirects its output to its own
    >>> input, and reads it line by line.


    And to clarify, what the Perl script is doing is redirecting the standard
    error to standard out. STDIN is file handle 0, STDOUT is file handle 1,
    and STDERR is file handle 2.
     
    Dan M, Jan 24, 2006
    #4
    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. Sudsy
    Replies:
    0
    Views:
    4,397
    Sudsy
    Jul 18, 2003
  2. Michael McGarry

    Redirecting stdin/stdout to a GUI widget

    Michael McGarry, Dec 17, 2004, in forum: C++
    Replies:
    5
    Views:
    538
    Michael McGarry
    Dec 18, 2004
  3. Jan Knop
    Replies:
    4
    Views:
    1,081
    Jan knob
    Nov 24, 2003
  4. Matthew Thorley

    Redirecting stdout and stdin

    Matthew Thorley, Mar 4, 2004, in forum: Python
    Replies:
    2
    Views:
    764
    Donn Cave
    Mar 4, 2004
  5. Michael McGarry
    Replies:
    1
    Views:
    629
    Michael Fuhr
    Dec 16, 2004
Loading...

Share This Page