Strpos() not working as expected

I’m using strpos() to check for a shell exec success and it’s failing to match the string and the if block is executed saying the command failed but it did not. When I test this using php interactively it works as expected but not in my script. Here’s the code snippet…

    $sum_out = `$sum -i $ipmi_ip -u \$SUPERMICROUSER -p \$SUPERMICROPW -c ChangeBiosCfg --file $bios_config_file`;

    $pos = strpos($sum_out, "Status: The BIOS configuration is updated");
    if ($pos === false) {
      print("FAIL\n\tERROR: BIOS config update on $blade was unsuccessfull\n");

var_dump($sum_out) =>

Copyright(C)2019 Super Micro Computer, Inc. All rights reserved.
....
Status: The BIOS configuration is updated for 10.140.239.176

Note: You have to reboot or power up the system for the changes to take effect"

When executed interactively, $pos = 197 as expected. Any ideas?
Thanks,
Rob

print $sum_out just to be sure you are getting what you’re expecting?

Is this the var dump interactive or in script? (I suspect Dave and I are having the same brainwave; that your script’s execution doesn’t have the privileges necessary to execute this function because it’s executed as the web daemon’s user instead.)

The var_dump() is executed within the script so I can see that the sum2.2 command is executing successfully. And since I’m passing a user and password, sum2.2 has the necessary perms.

Can you post the actual script vs what you’ve stripped out to post here (you can obscure usernames, passwords, ip addresses, etc)?? I’m asking because there is NO way to get from the first line to the second line of what you’ve posted in the OP without some steps in between.

In the sample you’ve posted here, $pos will be false because $sum_out has this

`$sum -i $ipmi_ip -u \$SUPERMICROUSER -p \$SUPERMICROPW -c ChangeBiosCfg --file $bios_config_file`;

I would expect to see a line more like this:

$sum_out = shell_exec(`$sum -i $ipmi_ip -u \$SUPERMICROUSER -p \$SUPERMICROPW -c ChangeBiosCfg --file $bios_config_file`);
    $sum_out = `$sum -i $ipmi_ip -u \$SUPERMICROUSER -p \$SUPERMICROPW -c UpdateBIOS --file $bios_file --reboot; powerctrl $ipmi_ip --on-if-off -c;`;
    fwrite($log, "$sum_out\n");
  
    $pos = strpos($sum_out, "Status: BIOS is updated");
    if ($pos === false) {
      print("FAIL\n\tERROR: BIOS update on $blade was unsuccessfull\n");
      fwrite($log, "FAIL\n\tERROR: BIOS update on $blade was unsuccessfull\n");
      var_dump($sum_out);
      exit;
    }
    print("SUCCESS\n");

EDIT: I knew there was something different. You’re going to kick yourself…

Your sample code in your OP:

$pos = strpos($sum_out, "Status: The BIOS configuration is updated");

The last sample you posted…

$pos = strpos($sum_out, "Status: BIOS is updated");

See the difference?

(Following is when I copy/pasted the string values from your OP to phptester.net)
There’s something else going on that I’m missing.

You’re going to need to var_dump $sum_out and $pos before the if statement, because when I run a dummy version of this over at phptester.net, it seems to work for me.

$blade="test";
$sum_out = "Copyright(C)2019 Super Micro Computer, Inc. All rights reserved." .
"...." .
"Status: The BIOS is updated for 10.140.239.176" .
"Note: You have to reboot or power up the system for the changes to take effect";

$pos = strpos($sum_out, "Status: The BIOS is updated");
var_dump($sum_out);var_dump($pos);

    if ($pos === FALSE) {
      print("FAIL\n\tERROR: BIOS update on $blade was unsuccessfull\n");
      var_dump($sum_out);
	} else {
		print("SUCCESS\n");
	}

results in

string(192) "Copyright(C)2019 Super Micro Computer, Inc. All rights reserved.....Status: The BIOS is updated for 10.140.239.176Note: You have to reboot or power up the system for the changes to take effect" int(68) SUCCESS

or without the var_dumps…

$blade="test";
$sum_out = "Copyright(C)2019 Super Micro Computer, Inc. All rights reserved." .
"...." .
"Status: The BIOS is updated for 10.140.239.176" .
"Note: You have to reboot or power up the system for the changes to take effect";

$pos = strpos($sum_out, "Status: The BIOS is updated");

    if ($pos === FALSE) {
      print("FAIL\n\tERROR: BIOS update on $blade was unsuccessfull\n");
	} else {
		print("SUCCESS\n");
	}

results in

SUCCESS

RESOLVED: I found the problem. Turns out there were special characters before or around “Status:”. So I changed the strpos() to search for “BIOS is updated”. Thanks for all who replied.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.