Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash command to read from network socket?

Tags:

bash

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?

like image 346
Jen S. Avatar asked Nov 26 '10 07:11

Jen S.


People also ask

How do I view a .socket file?

A socket is a file for processes to exchange data. You can see more data about it using the netstat , lsof , and fuser commands.

How do I open a socket in Linux?

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.

What is read R?

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).

How do I list sockets in Unix?

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.


2 Answers

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. :)

like image 108
Cipi Avatar answered Oct 07 '22 18:10

Cipi


Netcat is the tool usually used to do this, but it can also be done with the /dev/tcp and /dev/udp special pathnames.

like image 35
Ignacio Vazquez-Abrams Avatar answered Oct 07 '22 18:10

Ignacio Vazquez-Abrams