简体   繁体   中英

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. The snippet of George's code which does this is:

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? 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...

Octal dump in Linux shell without od or hd

Seems simple enough with 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. That included replicating the weird XOR, which seems to be something to do with rudimentary encryption (probably). I then pulled out the value which I needed using sed. Cross-compiling this C on my Lubuntu machine for the target CRIS architecture wasn't too difficult for a simple program. Everything was much easier once I'd reduced the model code to a minimal reproducible example for myself. Thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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