bash command to read from network socket?
Netcat is the tool usually used to do this, but it can also be done with the /dev/tcp
and /dev/udp
special pathnames.
The already mentioned netcat (nc
) is simple and powerful. But if you need a yet more powerful tool: socat.
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:
echo "GET /files/a_file.mp3 HTTP/1.0" | nc -w 5 192.168.0.2 999 > /tmp/the_file.mp3
Switch -w 5
states that nc will wait 5 seconds max for a response. When nc is done downloading, the socket is closed.
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:
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. :)