Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Redirecting stdin/stdout to self

Reply
Thread Tools

Redirecting stdin/stdout to self

 
 
Jan Danielsson
Guest
Posts: n/a
 
      01-23-2006
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.
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      01-23-2006
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
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      01-23-2006
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!
 
Reply With Quote
 
Dan M
Guest
Posts: n/a
 
      01-24-2006
>>> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing self: if self is a tree how to set to a different self Bart Kastermans Python 6 07-13-2008 11:19 AM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) falcon Python 0 07-31-2005 05:41 PM
Re: __autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 2 07-12-2005 03:20 AM
Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code Ralf W. Grosse-Kunstleve Python 16 07-11-2005 09:28 PM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 18 07-11-2005 04:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57