Skip to content

Instantly share code, notes, and snippets.

@fotile96
Forked from nuxlli/unix_socket_request.sh
Created January 17, 2024 16:07
Show Gist options
  • Save fotile96/17866b3df6d7b95720d8919096d8457b to your computer and use it in GitHub Desktop.
Save fotile96/17866b3df6d7b95720d8919096d8457b to your computer and use it in GitHub Desktop.
Examples of http request in unix domain socket with shell, using socat, netcat or curl
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment