bash 命令从网络套接字读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4283209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash command to read from network socket?
提问by Jen S.
I am looking for a simple bash command to open a client socket, read everything from the socket, and then close the socket. Something like wget or curl for sockets.
我正在寻找一个简单的 bash 命令来打开客户端套接字,从套接字读取所有内容,然后关闭套接字。像套接字的 wget 或 curl 之类的东西。
Is there a command in Bash to do this? Of do I need to write a bash script?
Bash 中是否有执行此操作的命令?我需要写一个 bash 脚本吗?
采纳答案by Ignacio Vazquez-Abrams
Netcatis the tool usually used to do this, but it can also be done with the /dev/tcpand /dev/udpspecial pathnames.
Netcat是通常用于执行此操作的工具,但也可以使用/dev/tcp和/dev/udp特殊路径名来完成。
回答by Cipi
Use nc. Its quick and easy. To connect to client 192.168.0.2 on port 999, send it a request for a resource, and save that resource to disk, do the following:
使用 nc。它快速简便。要在端口 999 上连接到客户端 192.168.0.2,向其发送对资源的请求,并将该资源保存到磁盘,请执行以下操作:
echo "GET /files/a_file.mp3 HTTP/1.0" | nc -w 5 192.168.0.2 999 > /tmp/the_file.mp3
echo "GET /files/a_file.mp3 HTTP/1.0" | nc -w 5 192.168.0.2 999 > /tmp/the_file.mp3
Switch -w 5states that nc will wait 5 seconds max for a response. When nc is done downloading, the socket is closed.
Switch-w 5声明 nc 最多等待 5 秒以获得响应。当 nc 完成下载时,套接字关闭。
If you want to send a more complex request, you can use gedit or some other text editor to write it, save it to file "reqest", and then cat that file through the pipe to nc:
如果你想发送一个更复杂的请求,你可以使用 gedit 或其他一些文本编辑器来编写它,将它保存到文件“reqest”,然后通过管道将该文件 cat 到 nc:
cat request.txt | nc -w 5 192.168.0.2 999 > /tmp/the_file.mp3
cat request.txt | nc -w 5 192.168.0.2 999 > /tmp/the_file.mp3
You don't need write a script for this, because it is a one line command... But if you will use it often, writing a script is a must!
你不需要为此编写脚本,因为它是一个单行命令......但如果你经常使用它,那么编写脚本是必须的!
Hope I helped. :)
希望我有所帮助。:)

