Convertion bash to php

liderbug

New member
OK, UNCLE -
The input od date is:
Code:
0000000  \0  \0 002   [ 320 362 201 370 213 377 232 367 325 357 224 266
0000020 321 264 300 237 354 225 346 217 341 207 350 312 360 213 251 332
0000040 255 362 204 341 223 261 213 251 230 266 206 250 235 275 377 212
0000060 343 217 353 313 371 313 372 312 370 311 351 273 336 262 234 255
0000100 225 246 222 242 226 264 230 272 322 245 372 214 351 233 271 203
0000120 241 220 276 216 254 200 242 317 240 304 241 315 357 325 367 262
0000140 342 323 343 313 236 315 344 306 352 310 254 311 277 326 265 320
...
The Bash script is:
Code:
#!/bin/bash

   code=171
   offset=4
   input_num=`od -j $offset -An -t u1 -v | tr "\n" " "`
   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"

I'm tired, lack of coffee, but converting it to PHP is just not working. Mainly it the "element ^ code" that's got me snowed. Can someone convert this - so I can slap my forehead - ah duh!
Output should be: {"system":{"get_sysinfo":{"sw_ver":"1.0.5 Build 2 ....

Thanks
 
Try to create a script with these steps:
1. Read the output to the array where each number is a separate element
2. Iterate array from 4th element ($offset)
3. Make a bit operation for each element to cinvert octal to decimal (octdec function) then change decimal to char (chr function)
4. Convert array to string (implode function)
5. Print output as a JSON
 
Google says that bash "^" is modulo. And for php it's "%" and my test script is giving different results. My head is hurting.
Just re-read your ans - Ta-Da! I'm pulling in an octal number and then doing module on a octal number - splat! <sigh>....
 
I was wrong. The "^" caret says XOR - as is in PHP and SOLVED.
Code:
$in = file_get_contents ("t1");
$od = array("");
$text = "";
foreach (str_split($in, 1) as $byte) {
  $od[] = ord($byte);
}
$od = array_slice($od, 5);
$code = 171;
foreach ($od as $ele)
{
  $out = $ele ^ $code;
  $text .= sprintf ("%c", $out);
  $code = $ele;
}
echo "$text\n";
 
Because I was unable to find what I needed - here's 90% of what someone else can use
Code:
#!/usr/bin/php

<?php

function OnOff ($ip, $oos) # OnOffStat
{
  $oosa =  base64_decode($oos);
  $stream = stream_socket_client("tcp://$ip:9999", $errno, $errstr);
  if (!$stream) {
    echo "{$errno}: {$errstr}\n";
    die();
  }
  fwrite($stream, $oosa);
  stream_socket_shutdown($stream, STREAM_SHUT_WR);
  $ret = stream_get_contents($stream);
  fclose($stream); #if close before ret= the ret=null
  return ($ret);
}

# ==== main ====
  $on = "AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu36Lfog==";
  $off = "AAAAKtDygfiL/5r31e+UtsWg1Iv5nPCR6LfEsNGlwOLYo4HyhueT9tTu3qPeow==";
  $status = "AAAAI9Dw0qHYq9+61/XPtJS20bTAn+yV5o/hh+jK8J7rh+vLtpbr";

# $ARGV and a switch()
# $ret = OnOff ("sg102", "$on"); # sg102 = 192.168.0.102 address of switch
# $ret = OnOff ("sg102", "$off"); # 
# $ret = OnOff ("sg102", "$status"); # one of these of a combo

$od = array("");
foreach (str_split($ret, 1) as $byte) {
  $od[] = ord($byte);
}

$od = array_slice($od, 5); # chuck the first 4 bytes

$text = "";
$code = 171;   # the starting code - don't ask
foreach ($od as $ele)
{
  $out = $ele ^ $code; # XOR with the next byte
  $text .= sprintf ("%c", $out);
  $code = $ele;
}
echo "$text\n";
 
The above code is because I wanted "KASA" app on my Fedora laptop as kasa.php that would look and act like the Kasa app on my Android phone.
 
Hope this helps:
PHP:
<?php

$code = 171;
$offset = 4;

// Read input from standard input
$inputNum = stream_get_contents(STDIN);
$array = array_map('intval', explode(' ', trim($inputNum)));

$argsForPrintf = '';
foreach ($array as $element) {
    $output = $element ^ $code;
    $argsForPrintf .= sprintf("\\x%02x", $output);
    $code = $element;
}

printf($argsForPrintf);
 
Back
Top