Linux – convert a hex string to binary and send with netcat

binarylinuxnetcat

I have a binary file that I can send with netcat:

$ nc -l localhost 8181 < my.dat

The file contains this:

$ xxd my.dat
0000000: 0006 3030 3030 4e43                      ..0000NC

What I really want to do is send the hex string directly. I've tried this:

$ echo '0006303030304e43' | nc -l localhost 8181

However, the above command just sends the ascii string directly to nc.

Best Answer

I used the -r and -p switch to xxd:

$ echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181

Thanks to inspiration from @Gilles answer, here's a perl version:

$ echo '0006303030304e43' | perl -e 'print pack "H*", <STDIN>' | nc -l localhost 8181
Related Question