Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Unix Domain Sockets from the command line?

Reading a Unix Domain Socket file using Python is similar to an ordinary 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 ordinary files, cat does not work on them:

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

Neither do curl:

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

How do I read or write to Unix Domain Sockets using standard command line tools like curl?

PS: In a strange coincidence, a curl patch was suggested very recently)

like image 939
Adam Matan Avatar asked Nov 28 '14 20:11

Adam Matan


People also ask

How do I find my domain socket Unix?

Examining Unix Domain Sockets. 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.

How do I interact with a UNIX socket?

Using this you can connect to either a UNIX-domain stream socket or a UNIX-domain datagram socket, and therefore you have to tell the socket's type to netcat. for example, /dev/log file in Linux is a UNIX-domain datagram socket socket, thus nc -U /dev/log won't work. Instead use nc -uU /dev/log .

What is Unix socket path?

UNIX domain sockets are named with UNIX paths. For example, a socket might be named /tmp/foo. UNIX domain sockets communicate only between processes on a single host.


Video Answer


1 Answers

You can use ncat command from the nmap project:

ncat -U /tmp/tbsocket1

To make it easy to 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
like image 187
kev Avatar answered Sep 17 '22 23:09

kev