Jump to content

Script fails if fread is great than 1


Boxerman
Go to solution Solved by 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
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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.