Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all. I have a question around FIFO files. I would like to read a FIFO and pipe it's contents to netcat. (In this case a printer, but it's the ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    17

    FIFO - Parsing and Piping

    Hi all.

    I have a question around FIFO files.

    I would like to read a FIFO and pipe it's contents to netcat. (In this case a printer, but it's the concept I am concerned with).

    I will be appending to this FIFO from an application.
    I had syslog in mind, but again, this is a POC.

    Initially, this is no problem.
    But after the first write, the solution fails.
    I think netcat is terminating (maybe due to the EOF?).

    #Example
    mkfifo /tmp/testfifo
    nc 192.168.1.1 9100 < /tmp/testfifo &
    echo Line1 > /tmp/testfifo
    #This line gets piped to nc and delivered.
    echo Line2 > /tmp/testfifo
    #This line is unable to write.

    Like I said, I think this maybe to do with the EOF.
    But I don't honestly know.

    I guess I need something that reads the FIFO, and executes a new process upon every write? Instead of expecting nc to be open for the whole time?

    I hope tail would do this, but no luck.

    Can anyone explain what is happening, or perhaps even provide me with a solution?

    This strikes me as something that may be a common problem/requirement under various parts of any Unix system.

    Any ideas?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    In some other window, do this:
    Code:
    cat > your_named_pipe
    and let it sit there. Then write single lines to it from elsewhere to your heart's content. As long as at least one process has that FIFO open for writing, the process that has it open for reading will not experience end of file.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    17
    Thanks wje_lf

    I didn't think about that, you were quite right...

    Sadly, I was not! It's not the EOF that is causing the problem.

    The remote node is terminating the TCP session after a moment of no input. (and therefore netcat terminates).

    I think I will need something that executes a new instance of the binary for each write/line/lf or something.

    Kinda like a tail, with an execute function!?

    Any ideas people?
    wje_lf?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If I'm understanding you correctly, on the destination host you pipe the netcat data to an application, and that application exits before you want it to, and you wish to fire up that application for each line that netcat wants to give it, without having to restart netcat. Ja?

    If so, try something like this.

    On the destination host, instead of piping netcat to the application directly, pipe it to a shell script something like the following, which I haven't tested.
    Code:
    #!/bin/sh
    
    while read
    do
      # Angels we have heard on high.  (sorry)
    
      echo "$REPLY" > your_application
    done
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Nov 2007
    Posts
    17
    Hi wje_lf,

    If I'm understanding you correctly, on the destination host you pipe the netcat data to an application, and that application exits before you want it to, and you wish to fire up that application for each line that netcat wants to give it, without having to restart netcat. Ja?
    Although that not exactly right, it was close enough!

    In this case I am just sending data to a network printer (TCP:9100)
    It is the printer that is terminating the TCP session.

    So I was unable to implement your script at the destination.

    However!......

    I was able to solve my problem using your techniques from client side.

    #Begin "/tmp/ncline"
    Code:
    #!/bin/sh
    while read
    do echo "$REPLY"|nc printerip 9100
    done
    #Start the "system"
    Code:
    cat > /tmp/testfifo &
    /tmp/ncline < /tmp/testfifo
    #Testing!
    Code:
    echo Line1 > /tmp/testfifo
    echo Line2 > /tmp/testfifo
    With this method I have been able to direct syslog data to my printer.
    (And also festival which was pretty funny!)

    So thanks goto wje_lf!

    Cheers bro!

    Keywords: syslog to printer
    Keywords: syslog to festival

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...