想要 Shell 魔法:在管道中格式化 hexdump 的输出

发布于 2024-11-07 05:29:10 字数 1166 浏览 0 评论 0原文

我正在调试通过 TCP 传输数据的程序的输出。 出于调试目的,我用 netcathexdump 替换了接收程序:

netcat -l -p 1234 | hexdump -C

它将所有数据输出为一个漂亮的十六进制转储,几乎就像我想要的那样。现在,数据以固定块的形式传输,其长度不是 16 的倍数,导致输出中的行发生移位,从而使发现差异变得有点困难:

00000000  50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00000010  00 50 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |.P..............|
00000020  00 00 50 00 00 00 00 00  00 00 00 00 00 00 00 00  |..P.............|

如何重新格式化输出,以便在 17 个字节后开始新行? 它应该看起来像这样:

50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |
50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |
50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |

使用 hexdumps -n 参数不起作用,因为它会在达到字节数后退出。 (除非有办法保持 netcat 程序运行并将下一个字节无缝管道传输到新的 hexdump 实例)。

另外,如果我可以在输出上使用 watch -d 来突出显示行之间的更改,那就太好了。

I'm debugging the output of a program that transmits data via TCP.
For debugging purposes i've replaced the receiving program with netcat and hexdump:

netcat -l -p 1234 | hexdump -C

That outputs all data as a nice hexdump, almost like I want. Now the data is transmitted in fixed blocks which lengths are not multiples of 16, leading to shifted lines in the output that make spotting differences a bit difficult:

00000000  50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00000010  00 50 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |.P..............|
00000020  00 00 50 00 00 00 00 00  00 00 00 00 00 00 00 00  |..P.............|

How do I reformat the output so that after 17 bytes a new line is started?
It should look something like this:

50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |
50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |
50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |

Using hexdumps -n parameter does not work since it will exit after reaching the number of bytes. (Unless there is a way to keep the netcat programm running and seamlessly piping the next bytes to a new instance of hexdump).

Also it would be great if I could use watch -d on the output to get a highlight of changes between lines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

菊凝晚露 2024-11-14 05:29:10

对于没有字符部分的十六进制转储。

hexdump -e '16/1 "%0.2x " "\n" 1/1 "%0.2x " "\n"'

For hexdump without characters part.

hexdump -e '16/1 "%0.2x " "\n" 1/1 "%0.2x " "\n"'

一世旳自豪 2024-11-14 05:29:10

我使用这个:

use strict;
use warnings;
use bytes;

my $N = $ARGV[0];

$/ = \$N;

while (<STDIN>) {
    my @bytes = unpack("C*", $_);
    my $clean = $_;
    $clean =~ s/[[:^print:]]/./g;
    print join(' ', map {sprintf("%2x", $_)} @bytes),
    "  |", $clean, "|\n";
}

将其作为 perl scriptname.pl N 运行,其中 N 是您想要的每个块中的字节数。

I use this:

use strict;
use warnings;
use bytes;

my $N = $ARGV[0];

$/ = \$N;

while (<STDIN>) {
    my @bytes = unpack("C*", $_);
    my $clean = $_;
    $clean =~ s/[[:^print:]]/./g;
    print join(' ', map {sprintf("%2x", $_)} @bytes),
    "  |", $clean, "|\n";
}

Run it as perl scriptname.pl N where N is the number of bytes in each chunk you want.

金兰素衣 2024-11-14 05:29:10

您也可以使用 xxd -p 制作十六进制转储。

also you can use xxd -p to make a hexdump .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文