V
V
Vladimir Kuts2020-04-16 08:43:56
linux
Vladimir Kuts, 2020-04-16 08:43:56

How to make netcat save data to a file?

There is a certain service that periodically sends packets to the server on a specific port. I want to hang netcat on it so that all incoming packets are saved to a given file in the background.
If run
nc -k -lp 8080 >> data.txt &

Then it takes one packet and exits.

If you run it like this: Then the process receives and writes one packet, displays something like:
while true; do nc -k -lp 8080 >> data.txt; done &


[2]+ Stopped while true; do
nc -k -lp 8080 >> data.txt;
done


continues to hang in the processes, but nothing is written to the file.
What is the correct way to start netcat so that it hangs in the background and continues to write packets to a file?

Debian GNU/Linux 9 system

PS For the time being, I solved it by launching it not in the background, but in screen. But I would like a more elegant solution.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2020-04-16
@fox_12

Decided on my own. This code works correctly, plus, as a bonus, timestamps are written in the log along with the package dump:

while true; do (date; nc -k -lp 8080 2>&1 | sed 's/\r//g') | xargs >> environ.txt; done &

G
Germanjon, 2020-04-16
@Germanjon

man tee

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question