Jump to content

Script fails if fread is great than 1


Boxerman

Recommended Posts

Hi guys,

 

Tricky one to explain here so first sorry if i do not provide everything.

 

The below script is set to create a new user by passing PHP variables into a powershell script. The powershell has been tested manually and works perfectly:

 //cmd to run
$runCMD = "powershell -Command './MasterScript.ps1 -ucr -udn \"$user\" -pas \"$pass\" -loc \"$loc\"'";
// exec a command and return a stream
$stream = ssh2_exec($connection, $runCMD);
// force PHP to wait for the output
stream_set_blocking($stream, true);
// read the output into a variable
$data = '';
while($buffer = fread($stream, 4096)) {
    $data .= $buffer;
}
// close the stream
fclose($stream);
// print the response
echo $data;

As you can see the fread leagth is set to 4096, the webpage just sits at refreshing but never compeletes, however the the user gets created, but the page does not return the echo'ed $data (it doesn't display anything it is just sitting at refreshing the page).

 

If i set this to 1, it works as expected, however does not show the full stack trace (completed output which contains the username generated which is what i need to get. I have tried setting this to 2 but it just keeps waiting for the script to finish, but i can confirm i see the users. So this doesnt appear to be a powershell issue, more of a php code issue. Can anyone see a mistake in the above? 

 

Let me know if you require any more information. 

Link to comment
https://forums.phpfreaks.com/topic/297693-script-fails-if-fread-is-great-than-1/
Share on other sites

Thanks for the reply, im looking at your suggestion, but im getting the following:

 

Warning: fread() expects parameter 1 to be resource, null given in /srv/www/htdocs/newuserphp/test.php on line 31

Notice: Undefined variable: buffer in /srv/www/htdocs/newuserphp/test.php on line 31

any advise?

When i revert back to my old code i get an output if the user exists (full stack trace of what i need), if i create a new user, it only shows part of the stack trace (i presume related to the value of 1 needing to be increased?) but if its set above on, it just gets stuck on refreshing and never returns anything, yet the script does exactly what its suppose to (powershell wise).

 

Hopefully this makes sense.

When I create a test stream, everything works perfectly.

$stream = fopen('data://text/plain,This is a sample stream of text', 'r');

$data = '';
while(feof($stream) === false) {
    $data .= fread($stream, 4096);
}

echo $data;
I get "This is a sample stream of text" output.

 

I would suggest inspecting $stream to make sure that it is in fact what you think it is, and also to make sure error reporting is turned on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.