-
-
Save bodsch/209c079bbd87748e0a2d0c590304cbfe to your computer and use it in GitHub Desktop.
Bash: Socket Programming (Alternative to using Netcat)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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