Boxerman Posted August 8, 2015 Share Posted August 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 You have an infinite loop. It should be like this: while(feof($stream) === false) { $data .= fread($buffer, 4096); } Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 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 31Notice: Undefined variable: buffer in /srv/www/htdocs/newuserphp/test.php on line 31 any advise? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 8, 2015 Share Posted August 8, 2015 Replace $buffer with $stream Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 Oops, my bad. Updated: while(feof($stream) === false) { $data .= fread($stream, 4096); } Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 Thanks! however, the page is not outputting (its still refreshing) but when i check AD i see the new user account has been created!? This is confusing me, so much, im sorry! Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 (edited) Are you sure that $stream is what you think it is? And do you have error reporting on? Edited August 8, 2015 by scootstah Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 Yes that makes sense that it would refresh and never return anything, because you had an infinite loop going. Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 But with the new code provided with the feof in, it does exactly the same? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 8, 2015 Share Posted August 8, 2015 (edited) nm! Edited August 8, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 nm! No, feof() returns true once it reaches the end of the file. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 8, 2015 Share Posted August 8, 2015 I know. I usually use !feof() and was confusing myself. Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 How would one work out if stream is the correct one? Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 Something I just thought of: you can try stream_get_contents instead of all the fread stuff. $stream = ssh2_exec($connection, $runCMD); echo stream_get_contents($stream);See if that works. Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 New code: $stream = ssh2_exec($connection, $runCMD); stream_set_blocking($stream, true); echo stream_get_contents($stream); Still just stays at refreshing its so confusing, im sorry all... Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 8, 2015 Share Posted August 8, 2015 I would say it probably has to do with the blocking, then. The blocking says that PHP has to wait for output, so it seems to be stuck there. Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 8, 2015 Author Share Posted August 8, 2015 (edited) Any advise on what one would do to work around this issue? OR is there another way? Edited August 8, 2015 by Boxerman Quote Link to comment Share on other sites More sharing options...
Boxerman Posted August 10, 2015 Author Share Posted August 10, 2015 Anyone got advise? Quote Link to comment Share on other sites More sharing options...
Solution Boxerman Posted August 10, 2015 Author Solution Share Posted August 10, 2015 Fixed the issue: Adding [[ \"$-\" != *i* ]] && echo '\n' before the Powershell command fixed this issue. Thanks for all the help and i hope my fix helps someone one day! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.