Skip to content

Instantly share code, notes, and snippets.

@nuxlli
Last active April 27, 2023 03:09
Embed
What would you like to do?
Examples of http request (in unix domain socket) with bash and [nc|socat]
#!/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 version
echo -e "GET /images/json HTTP/1.1\r\n" | socat unix-connect:/var/run/docker.sock STDIO
# nc version (netcat-freebsd)
echo -e "GET /images/json HTTP/1.0\r\n" | nc -U /var/run/docker.sock
# bonus round: relay socket
socat -d -d TCP-L:8080,fork UNIX:/var/run/docker.sock
@psi-4ward
Copy link

echo -e "GET /services HTTP/1.0\r\n" | sudo nc -U /var/run/docker.sock | tail -n 1 | jq

@pauldraper
Copy link

pauldraper commented Sep 3, 2017

(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

@rija
Copy link

rija commented Aug 11, 2018

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.

@Zibri
Copy link

Zibri commented Dec 12, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment