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