Unix & Linux: convert a hex string to binary and send with netcat (4 Solutions!!)

preview_player
Показать описание
Unix & Linux: convert a hex string to binary and send with netcat

The Question: I have a binary file that I can send with netcat:
The file contains this:
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.

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 61 people ==
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

== This solution helped 14 people ==
If you have xxd, that's easy: it can convert to and from hexadecimal.
echo '0006303030304e43' | xxd -r -p | nc -l localhost 8181
I don't think there's a reasonable (and reasonably fast) way to convert
hexadecimal to binary using only POSIX tools. It can be done fairly easy in
Perl. The following script converts hexadecimal to binary, ignoring any input
character that isn't a hexadecimal digit. It complains if an input line
contains an odd number of hexadecimal digits.
#!/usr/bin/env perl
$^W = 1;
$c = undef;
while (<>) {
tr/0-9A-Fa-f//cd;
if (defined $c) { warn "Consuming $c"; $_ = $c . $_; $c = undef; }
if (length($_) & 1) { s/(.)$//; $c = $1; }
print pack "H*", $_;
}
if (!eof) { die "$!"; }
if (defined $c) { warn "Odd number of hexadecimal digits"; }
If you really need to stick to POSIX (e.g. on an embedded device), I recommend
uudecode to decode Base64. The input must have the header format and end line
produced by uuencode, it can't be raw Base64.
uudecode <<EOF | nc -l localhost 8181
begin-base64 644 -
AAYwMDAwTkM=
====
EOF

== This solution helped 26 people ==
Here a solution without xxd or perl:
If the echo builtin of your shell supports it (bash and zsh do, but not dash),
you just need to use the right backslash escapes:
echo -ne 'x00x06x30x30x30x30x4ex43' | nc -l localhost 8181
standard on Linux systems) or from busybox you can use it, too.
With sed you can generate a escaped pattern:
$ echo '0006303030304e43' | sed -e 's/../\x&/g'
x00x06x30x30x30x30x4ex43
Combined:
echo -ne "$(echo '0006303030304e43' | sed -e 's/../\x&/g')" | nc -l localhost
8181

Рекомендации по теме
join shbcf.ru