Skip to content

Instantly share code, notes, and snippets.

@bodsch
Forked from CMCDragonkai/bash_sockets.sh
Created April 19, 2018 07:52
Show Gist options
  • Save bodsch/209c079bbd87748e0a2d0c590304cbfe to your computer and use it in GitHub Desktop.
Save bodsch/209c079bbd87748e0a2d0c590304cbfe to your computer and use it in GitHub Desktop.
Bash: Socket Programming (Alternative to using Netcat)
#!/usr/bin/env bash
# consider if the server is passing a file like
while true; do nc -l 10000 <<<"hi"; done
# on our client side, we can consume this using nc
nc 10.0.0.1 10000
# however, if our client didn't have nc, we could easily do this bash socket feature
cat < /dev/tcp/10.0.0.1/10000
echo "test" > /dev/tcp/10.0.0.1/10000
# there's also /dev/udp
# note that bash cannot establish listening servers, it can only act as a client to an existing server
# see: http://unix.stackexchange.com/a/49947/56970
# this feature only applies to bash, and is not portable, not even to zsh
# and also they are not real files, they don't exist on the filesystem and they can only be used in redirections (the path cannot should not be passed to a program)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment