Redirecting stdin/stdout to self

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Danielsson

    #1

    Redirecting stdin/stdout to self

    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.
  • Grant Edwards

    #2
    Re: Redirecting stdin/stdout to self

    On 2006-01-23, Jan Danielsson <jan.danielsson @gmail.com> wrote:
    [color=blue]
    > 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.[/color]
    [...][color=blue]
    > ...however, the Perl script solution looks interresting.. Is it
    > possible to do something like that in Python?[/color]

    os.popen()




    --
    Grant Edwards grante Yow! Yow! I threw up on
    at my window!
    visi.com

    Comment

    • Grant Edwards

      #3
      Re: Redirecting stdin/stdout to self

      On 2006-01-23, Grant Edwards <grante@visi.co m> wrote:[color=blue]
      > On 2006-01-23, Jan Danielsson <jan.danielsson @gmail.com> wrote:
      >[color=green]
      >> 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.[/color]
      > [...][color=green]
      >> ...however, the Perl script solution looks interresting.. Is it
      >> possible to do something like that in Python?[/color]
      >
      > os.popen()
      >
      > http://www.python.org/doc/current/lib/os-process.html[/color]

      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!

      Comment

      • Dan M

        #4
        Re: Redirecting stdin/stdout to self

        >>> And if I'm reading it correctly, the Perl[color=blue][color=green][color=darkred]
        >>> script's process starts tcpdump, but redirects its output to its own
        >>> input, and reads it line by line.[/color][/color][/color]

        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.

        Comment

        Working...