Use nc to write multiple files - how?

耗尽温柔 提交于 2019-12-04 06:19:17

问题


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…


回答1:


On the receiver:

$ nc -p 3000 -l | tar -x

On the sender:

$ tar -c * | nc <ip_address> 3000



回答2:


Omit the -k and run it in a loop:

n=0
while nc -l 3000 > "$n".txt ; do
   n=$((n+1))
done



回答3:


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



来源:https://stackoverflow.com/questions/26356646/use-nc-to-write-multiple-files-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!