Sending binary data to a TCP or UDP connection in bash


#1

Sometimes you may want to send binary data to a TCP or UDP connection. You can send text data to a TCP port with telnet command but it doesn’t work for binary streams. There are some other alternatives which you can use like netcat or socat but it is also possible to send any data from TCP or UDP with just using bash shell’s internal functionality. For example to send text data to 192.168.2.5’s port 80 you can use:

$ echo "GET / HTTP/1.0" > /dev/tcp/192.168.2.5/80

or to send 160 bytes of a binary data to port 9000:

$ dd if=binary.dat bs=160 count=1 > /dev/tcp/192.168.2.5/9000

it is also possible to use udp too:

$ dd if=binary.dat bs=160 count=1 > /dev/udp/192.168.2.5/9000

Please note that, there is no /dev/tcp or /dev/udp folder exists in the system, this redirection mechanism is a feature of the bash shell itself.