想要 Shell 魔法:在管道中格式化 hexdump 的输出
我正在调试通过 TCP 传输数据的程序的输出。 出于调试目的,我用 netcat
和 hexdump
替换了接收程序:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于没有字符部分的十六进制转储。
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"'
我使用这个:
将其作为
perl scriptname.pl N
运行,其中 N 是您想要的每个块中的字节数。I use this:
Run it as
perl scriptname.pl N
where N is the number of bytes in each chunk you want.您也可以使用
xxd -p
制作十六进制转储。also you can use
xxd -p
to make a hexdump .