UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-04-14 09:02 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: redirecting one descriptor to many other descriptors

Q1: how can i do this?
      example: cat somefile.out 1>file1.out 1>file2.out
      the problem is that only file2.out is written
      file1.out is empty

Q2: the problem i have is this:
      a process creates tcp connections
      this process starts other scripts which handle the connections
     
      example: process1 connect -> google.com (desciptor 5)
                   process1 starts -> system("googlescript.php");
     
      what we now have, is that googlescript.php is running and desc 5 is open for reading and writing to google
      everything works perfectly, but i need something like logging
      the problem i have is, some scripts don't work correclty, so i need to log, what is sent/received during the connection

       this means i would have to do something like this
       system("googlescript.php 5<>/tmp/googlelog.out");
       2.1: the above system call is an example, but anybody has an idea, how can i log everything, that is sent/received to a file but without influencing the connection? (maybe with the tee command, but i have no idea how)
       2.2: simpler: 5<>/tmp/googlelog.out would overwrite googlelog.out everytime the script is started
              the script can run many times at one time (many processes)
             how can i append using <> ?  would this work? 5<<>>/tmp/googlelog.out

btw: because everything is running, and i only would need the logging between the php script and e.g. google
if you ask: why do use another script instead of doing it within "process 1"?
it's because of the flexibility
i can replace googlescript.php and the "server" is logically seperated by the "worker" (and thats what i need)

so the main question: how can i log a network connection using bash redirection, without influencing the connection itself?

[email protected]

Offline

#2 2006-04-14 09:57 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: redirecting one descriptor to many other descriptors

there is a useful command for doing this called tee.

You can make multiple redirections with this command i advice you to read the manual page.

but the command you tried with:

cat somefile.out 1>file1.out 1>file2.out


is false.

You can do that with tee like this:

cat test.txt | tee  test1 > test2

will fill test1 and test2 with the content of test.txt


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#3 2006-04-14 02:54 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: redirecting one descriptor to many other descriptors

Offline

#4 2006-04-14 03:00 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: redirecting one descriptor to many other descriptors

Yep, you can use tee to do it...  But, personally, for that example case, I'd just
do two separate "cat" calls; or, actually, for that specific example, it looks more like
"cp" would be the more appropriate command... ;-)  But, I'm guessing that was
just a simplistic example, and you have some more realistic use for it?

As for your second question of logging all traffic flowing over an FD, I'm not sure
you can really do that transparently with only simple shell redirections and such...
You could maybe do it with a separate FD and a relay/logger process of some
sort...  With just standard shell utils, I think it'd require 2 separate extra FDs (one
for input, one for output), and 2 separate incarnations of "tee", along with 2
FIFOs (named pipes)...  Ie: something like:

# FD 5 is the real socket; FD 6 and 7 are the new ones that the script will
# have to read from and write to in order for the logging to work
mkfifo fifo.in fifo.out
tee log.input <&5 > fifo.in &
tee log.output < fifo.out >&5 &
exec 6<fifo.in 7>fifo.out

# continue with the rest of the script, reading from FD 6, writing to FD 7
echo "Nybble me, Byte me, Unsigned long int me!" >&7
read junk <&6
#...

But, that's really ugly...

Offline

#5 2006-04-14 03:09 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: redirecting one descriptor to many other descriptors

Simulpost... ;-/

Well, if you've got control over the script, then the easiest thing to do is to add
"tee" in a pipeline with all I/O commands...  Eg: for your example:

echo -e "GET / HTTP/1.0\n" | tee -a log.out >&5
cat <&5 >>log.in

(The latter doesn't need a "tee", since you can just use the useless "cat" instead...)

But, if you want to do it transparently to the script, I'm not sure it's really going to
be possible beyond the ugly method I stated above, or having some separate
custom relayer/logger process...

Offline

#6 2006-04-14 03:39 PM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: redirecting one descriptor to many other descriptors

hmm sorry but i think i can't help you anymore...i'm not sure i really undertsand whay you exactly want to do...


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#7 2006-04-18 10:16 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: redirecting one descriptor to many other descriptors

@biologz
i think RobSeace first answer is what i was searching for
but thx

Offline

#8 2006-04-19 08:19 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: redirecting one descriptor to many other descriptors

hehe Rob Seace is wonderful lool i even quote him in my signature :-)


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

Board footer

Powered by FluxBB