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.
Is there a command in Bash to do this? Of do I need to write a bash script?
A socket is a file for processes to exchange data. You can see more data about it using the netstat , lsof , and fuser commands.
You can use the ss command to display open ports via listening sockets. This will print all listening sockets ( -l ) along with the port number ( -n ), with TCP ports ( -t ) and UDP ports ( -u ) also listed in the output.
Overview. The goal of readr is to provide a fast and friendly way to read rectangular data from delimited files, such as comma-separated values (CSV) and tab-separated values (TSV).
To list all listening Unix Domain Sockets, run the ss -xln command. The x flag ensures that only domain sockets are displayed. Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process . . . u_str LISTEN 0 5 /tmp/stream.
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. :)
Netcat is the tool usually used to do this, but it can also be done with the /dev/tcp
and /dev/udp
special pathnames.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With