简体   繁体   English

Linux shell 中的八进制转储,没有 od 或 hd

[英]Octal dump in Linux shell without od or hd

I'm trying to control a Kasa Smartplug from an Axis embedded Linux product by following what George Georgovassilis has done here: https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-hs100-wi-fi-smart-plug/ I've managed to switch the plug on and off from the Axis box but I've come unstuck trying to query the on/off status of the Smartplug because I don't have od (or hd, hexdump or xxd) and the Smartplug output is binary.我正在尝试按照 George Georgovassilis 在此处所做的操作从 Axis 嵌入式 Linux 产品中控制 Kasa Smartplug: https://blog.georgovassilis.com/2016/05/07/controlling-the-tp-link-h wi-fi-smart-plug/我已经设法从 Axis 盒子打开和关闭插头,但我在尝试查询 Smartplug 的开/关状态时遇到了困难,因为我没有 od(或 hd , hexdump 或 xxd) 和 Smartplug output 是二进制的。 The snippet of George's code which does this is:执行此操作的 George 代码片段是:

decode(){
    code=171
    input_num=`od $ODOPTS`
    IFS=' ' read -r -a array <<< "$input_num"
    args_for_printf=""
    for element in "${array[@]}"
    do
        output=$(( $element ^ $code ))
        args_for_printf="$args_for_printf\x$(printf %x $output)"
        code=$element
    done
    printf "$args_for_printf"
 }

Is there a way I can do this using basic shell commands instead of using od please?有没有办法我可以使用基本的 shell 命令而不是使用 od 来做到这一点? The Axis box says it's Linux 2.6.29 on a crisv32 I used to use Unix about 30 years ago so I'm struggling... Axis 盒子说它是 crisv32 上的 Linux 2.6.29 我大约 30 年前曾经使用 Unix 所以我很挣扎......

Octal dump in Linux shell without od or hd Linux shell 中的八进制转储,没有 od 或 hd

Seems simple enough with awk .使用awk似乎很简单。 Borrowing code from this answer and splitting from this answer it's simple to:从这个答案中借用代码并从这个答案中拆分出来很简单:

awk -v ORS='' -v OFS='' 'BEGIN{ for(n=0;n<256;n++)ord[sprintf("%c",n)]=n }  
    { split($0, chars, "");
    chars[length($0)+1]=RS; # add the newline to output. One could check if RS is empty.
    # and print
    for (i=1;i<=length($0)+1;++i) printf("%o\n", ord[chars[i]]) }'

I managed to solve this in the end: I couldn't work out how to replicate od using just the shell commands available on the target machine, so I created a very simple C program to read each byte from the binary and print it out as a readable character.我最终设法解决了这个问题:我无法弄清楚如何仅使用目标机器上可用的 shell 命令来复制 od,因此我创建了一个非常简单的 C 程序来读取二进制文件中的每个字节并将其打印为一个可读的字符。 That included replicating the weird XOR, which seems to be something to do with rudimentary encryption (probably).这包括复制奇怪的 XOR,这似乎与基本加密有关(可能)。 I then pulled out the value which I needed using sed.然后我使用 sed 提取了我需要的值。 Cross-compiling this C on my Lubuntu machine for the target CRIS architecture wasn't too difficult for a simple program.在我的 Lubuntu 机器上为目标 CRIS 架构交叉编译这个 C 对于一个简单的程序来说并不太难。 Everything was much easier once I'd reduced the model code to a minimal reproducible example for myself.一旦我将 model 代码简化为我自己的最小可重现示例,一切都会变得容易得多。 Thanks all.谢谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM