-
-
Save nuxlli/7553996 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# References | |
# http://www.computerhope.com/unix/nc.htm#03 | |
# https://github.com/daniloegea/netcat | |
# http://unix.stackexchange.com/questions/26715/how-can-i-communicate-with-a-unix-domain-socket-via-the-shell-on-debian-squeeze | |
# http://unix.stackexchange.com/questions/33924/write-inside-a-socket-open-by-another-process-in-linux/33982#33982 | |
# http://www.linuxjournal.com/content/more-using-bashs-built-devtcp-file-tcpip | |
# http://www.dest-unreach.org/socat/ | |
# http://stuff.mit.edu/afs/sipb/machine/penguin-lust/src/socat-1.7.1.2/EXAMPLES | |
# http://ubuntuforums.org/showthread.php?t=828870 | |
# Socat (tested: Linux and macOS) | |
echo -e "GET /v1.18/containers/json HTTP/1.1\r\nhost: localhost\r\n\r\n" | socat unix-connect:/var/run/docker.sock STDIO | |
# netcat (Linux) (netcat-freebsd or netcat-openbsd for Debian) | |
echo -e "GET /v1.18/containers/json HTTP/1.1\r\nhost: localhost\r\n\r\n" | nc -U /var/run/docker.sock | |
# netcat (macOS) (gnu netcat from homebrew is not supported) | |
echo -e "GET /v1.18/containers/json HTTP/1.1\r\nhost: localhost\r\n\r\n" | /usr/bin/nc -U /var/run/docker.sock | |
# curl version | |
curl --silent --unix-socket /var/run/docker.sock http://v1.18/containers/json | |
# bonus round: relay socket (unix sock -> tcp) | |
socat -d -d TCP-L:8080,fork UNIX:/var/run/docker.sock | |
# (tcp -> unix sock) | |
socat -d -d UNIX-L:/var/run/httpbin.sock,fork TCP:httpbin.org:80 |
(1) You need an extra \r\n
to indicate end of headers.
(2) HTTP RFCs don't consider half-closed connections, and many server implementations don't support them. So you may need to keep the socket open.
printf 'GET /images/json HTTP/1.0\r\n\r\n' | nc -q 2 -U /var/run/docker.sock
To get nc -U /var/run/docker.sock
working on Debian Jessie, you need to install the netcat-openbsd
package because the default netcat
package automatically install netcat-traditional
that doesnt support Unix Socket.
I get
HTTP/1.1 400 Bad Request: missing required Host header
Content-Type: text/plain; charset=utf-8
Connection: close
but printf 'GET /images/json HTTP/1.0\r\n\r\n' | nc -q 2 -U /var/run/docker.sock
returns
HTTP/1.0 200 OK
Api-Version: 1.37
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/18.03.1-ce (linux)
Date: Wed, 12 Dec 2018 08:43:45 GMT
Content-Length: 3
FYI: you can also use curl like: curl --silent --unix-socket /var/run/docker.sock http://v1.41/containers/json
.
If you have a payload you can read it first from stdin:
body=$(cat -)
curl --silent --unix-socket /var/run/docker.sock
-X POST http://v1.41/containers/create
-H 'Content-Type: application/json'
-d "$body"
But on the OpenWrt prior to Mar 27 2023 the curl is compiled without UNIX sockets. To check it use curl -V
.
On Ubuntu it will have Features: UnixSockets
but on the OpenWrt you won't see it.
echo -e "GET /services HTTP/1.0\r\n" | sudo nc -U /var/run/docker.sock | tail -n 1 | jq