How to access Unix Domain Sockets from the command line?

Reading a Unix Domain Socket file using Python is similar to a regular TCP socket:

>>> import socket >>> import sys >>> >>> server_address = '/tmp/tbsocket1' # Analogous to TCP (address, port) pair >>> sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) >>> sock.connect(server_address) >>> sock.recv(512) '*** uWSGI Python tracebacker output ***\n\n' 

Since UDS are not regular files, cat does not work with them:

 $ sudo cat /tmp/tbsocket1 cat: /tmp/tbsocket1: No such device or address 

Nobody does curl :

 $ sudo curl /tmp/tbsocket1 curl: (3) <url> malformed 

How to read or write to Unix domain sockets using standard command line tools like curl?

PS: By a strange coincidence, a patch for curls was recently proposed)

+11
linux unix curl unix-socket unix-domain-sockets
source share
2 answers

You can use the ncat command in the nmap project:

 ncat -U /tmp/tbsocket1 

To simplify access, you can do this:

 # forward incoming 8080/tcp to unix socket ncat -vlk 8080 -c 'ncat -U /tmp/tbsocket1' # make a http request via curl curl http://localhost:8080 

You can also use socat :

 # forward incoming 8080/tcp to unix socket socat -d -d TCP-LISTEN:8080,fork UNIX:/tmp/tbsocket1 
+19
source share

nc -U </unix/socket> almost any Linux, BSD, MacO.

+2
source share

All Articles