Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active July 6, 2022 18:11
Show Gist options
  • Save CMCDragonkai/16239c073d9937912523 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/16239c073d9937912523 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)
@gvasconcelos
Copy link

I love you.

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