Use nc to write multiple files - how?

2020-05-08 08:34发布

I want to use nc as a simple TCP/IP server. So far I run it using:

$ nc -k -l 3000 > temp.tmp

This works, but writes all the received data of all connections into one single file. But, I would like to have individual files.

Basically I get this if I skip the -k switch, but then nc shuts down as soon as the first connection is gone.

How can I keep nc running, but use an individual file for each incoming request? (Or, if this is not possible, is there an alternative *nix tool that is able to do this?)

Please note that I do not want to restart nc, but I want it all with a single running instance.

PS: I know that SO doesn't allow questions on finding tools, but for me this is only the fallback in case nc isn't able to do that by itself. So, sorry for the part in parentheses…

标签: bash netcat
3条回答
Rolldiameter
2楼-- · 2020-05-08 09:10

I think Cronolog can help in this case

-p option you can determine the period option

Also the filewatcher utility "incron" can be used which will check logs to one file & can split to some other files

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-05-08 09:14

Omit the -k and run it in a loop:

n=0
while nc -l 3000 > "$n".txt ; do
   n=$((n+1))
done
查看更多
成全新的幸福
4楼-- · 2020-05-08 09:15

On the receiver:

$ nc -p 3000 -l | tar -x

On the sender:

$ tar -c * | nc <ip_address> 3000
查看更多
登录 后发表回答