3

From time to time I want to send some text to the input of netcat. Netcat will send it then to another netcat process (either on my machine or a local network), but now I am only concerned with the first one.

E.g. on keypress the user fills a value in a prompt; I want to send that value to that process.


Given the value in, say $text, how do I send it to netcat so that it receives it and treats it just like normal stdin?


I've tried the following ways, none of them work:

  • echo $text >> /proc/`pidof netcat`/fd/0.
    This writes $text to the terminal but netcat does not receive it. (according to this question)

    • I also tried starting netcat like this: cat | netcat ... and echo-ing to the cat process
  • mkfifo pipe; netcat <mypipe
    Now, when the other netcat process connects, it receives what I have sent to mypipe so far.
    Any further writes to mypipe have no effect.

    • I also tried cat mypipe | netcat ...
      This had a different result: the first write to mypipe is sent to the other netcat process, but the second echo 123 >mypipe attempt freezes - I do not get the prompt and I can enter values but nothing happens.

As I just learned about named pipes, I might not be using the correctly.
But looking for "stream files" or simmilar tells me they are the right tool for this job (but not how).

The alternative to my first attempt (I learned about this on serverfault) was to use screen but I think it's overkill for my problem.

The issue with the named pipes seems to be that echo sends EOF after the string. In turn, netcat stopps reading from the pipe and therefore I can't send any more messages (I can, but netcat doesn't read them). This led me to Piping data to a process's stdin without causing EOF afterward, but I don't see how to apply the provided solution to my case.

I think I covered any possible combination of pipes/cats/echoes without result so the only thing left was unix.se :)

Thanks in advance!

Al.G.
  • 269
  • 1
  • 3
  • 14

0 Answers0