shell_exec in PHP returns empty string - php

shell_exec and exec are not returning any content. I can't figure out what's wrong.
Here's some code:
echo 'test: ';
$output = shell_exec('whoami');
var_export($output, TRUE);
echo PHP_EOL . '<br>' . PHP_EOL;
And here's the source of the output
test 2:
<br>
I do not have control over the host, but I believe they're running SuPHP. According to phpinfo, safe_mode is off. Running whoami from SSH outputs the expected value.
I'm at a loss. Any idea how to debug this?

You're never printing the $output variable. The var_export() call returns the content of the variable when you call it with a true second parameter, it does not print it directly.

If you want the output from a shell command read back into PHP, you're probably going to need popen(). For example:
if( ($fp = popen("some shell command", "r")) ) {
while( !feof($fp) ) {
echo fread($fp, 1024);
flush(); // input will be buffered
}
fclose($fp);
}

Related

PHP ssh2_exec no feof on empty stdout

This PHP snippet should execute a simple command via SSH (stripped down to minimal working example):
$sshconn = ssh2_connect($HostAddr, 22);
ssh2_auth_pubkey_file($sshconn, $user, $sshkey . '.pub', $sshkey);
$stdout = ssh2_exec($sshconn, 'echo hello');
if ($stdout !== false)
{
stream_set_blocking($stdout, true);
while (!feof($stdout))
{
$proc_stdout = fgets($stdout, 3E6);
if ($proc_stdout !== false) echo $proc_stdout;
}
fclose($stdout);
}
Works great as long as there is any output to stdout. However, if stdout remains empty, the loop turns into an endless loop.
$stdout = ssh2_exec($sshconn, 'echo hello >&2');
How do I read the stdout properly if
stdout may be empty
but stdout may also very large (several Gigabytes, impossible to slurp into a variable by a single call to stream_get_contents or the like).
Addendum: My real world code that hangs executes mysqldump with an unknown parameter. Obviously, echo -n >&2 works as expected, although its stdout is also empty.
If anyone stumbles upon this too: stderr has to be read too.
$sshconn = ssh2_connect($HostAddr, 22);
ssh2_auth_pubkey_file($sshconn, $user, $sshkey . '.pub', $sshkey);
$stdout = ssh2_exec($sshconn, 'echo Hello >&2');
if ($stdout !== false)
{
$stderr = ssh2_fetch_stream($stdout, SSH2_STREAM_STDERR);
stream_set_blocking($stdout, false);
stream_set_blocking($stderr, false);
while (!feof($stdout))
{
$proc_stdout = fgets($stdout, 3E6);
if ($proc_stdout !== false) echo $proc_stdout;
$proc_stderr = fgets($stderr, 3E6);
if ($proc_stderr !== false) fwrite(STDERR, $stderr);
}
fclose($stdout); fclose($stderr);
}
The disadvantage of this is that the SSH connection is no longer usable afterwards (a further ssh2_exec cannot be executed).
A possible solution is to keep both streams blocking and read stdout and stderr non-interleaved:
$stdout = ssh2_exec($sshconn, 'echo Hello >&2');
$stderr = ssh2_fetch_stream($stdout, SSH2_STREAM_STDERR);
stream_set_blocking($stdout, true);
stream_set_blocking($stderr, true);
while ($content = fread($stdout, 3E6)) echo $content;
while ($content = fread($stderr, 3E6)) fwrite(STDERR, $content);
I invite anyone who knows how to have the streams interleaved and keep the SSH connection usable to post another answer.

fsockopen - how to detect end of message?

I have code:
$f = fsockopen('mail.myserver.com', 110); //POP3
echo fgets($f, 4096) . '<hr>';
fputs($f, "USER login#myserver.com\r\n");
echo fgets($f) . '<br>';
fputs($f, "PASS mypass\r\n");
echo fgets($f) . '<br>';
fputs($f, "LIST\r\n");
echo fgets($f) . '<br>';
fputs($f, "RETR 1\r\n");
So far so good, but then I have a loop to receive the whole message:
while (!feof($f))
{
echo fgets($f, 1280);
}
and it takes forever because the script waits for timeouts to kick in—it NEVER detects EOF by itself.
How can I detect EOF and break the loop before it times out? If I use telnet then everything works—somehow telnet clients know when to stop receiving.
See:
Warning If a connection opened by fsockopen() wasn't closed by the server, feof() will hang. To workaround this, see below example:
On: feof
The workaround essentially waits default_socket_timeout and then terminates the while-loop.
Sockets may provide a better alternative to handling timeouts/hangs.

fprint doens't work in my php

I want to write some text from script in browser.
<?php
$out = fopen('php://stdout', 'w');
fprintf($out, "Hello!");
fclose($out);
?>
I expect "Hello!" on output, but nothing happens...Could it be bug in stdout, or I just oversighted something?
This would be true if calling php from console.
But if you are talking about web access, you should use php://output
Have you tried the simple :
<?php
$out = "foobar";
echo $out;
?>
?
By default, php print on STDOUT.

Error Reporting? Import .sql file from PHP and show errors

I am building a way of importing .SQL files into a MySQL database from PHP. This is used for executing batches of queries. The issue I am having is error reporting.
$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file";
exec($command, $output);
This is essentially how I am importing my .sql file into my database. The issue is that I have no way of knowing if any errors occurred within the PHP script executing this command. Successful imports are entirely indistinguishable from a failure.
I have tried:
Using PHP's sql error reporting functions.
Adding the verbose argument to the command and examining the output. It simply returns the contents of the .sql file and that is all.
Setting errors to a user variable within the .sql file and querying it from the PHP script.
I hope I am not forced to write the errors into a temporary table. Is there a better way?
UPDATE:
If possible, it would be very preferable if I could determine WHAT errors occurred, not simply IF one occurred.
$command = "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname"
. " < $file 2>&1";
exec($command, $output);
The error message you're looking for is probably printed to stderr rather than stdout. 2>&1 causes stderr to be included in stdout, and as a result, also included in $output.
Even better, use proc_open instead of exec, which gives you far more control over the process, including separate stdout and stderr pipes.
Try using shell_exec
$output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file" );
// parse $output here for errors
From the manual:
shell_exec — Execute command via shell and return the complete output as a string
Note:
This function is disabled when PHP is running in safe mode.
EDIT: Full solution:
what you need to do is grab STDERR and discard STDOUT. Do this by adding '2>&1 1> /dev/null' to the end of your command.
$output = shell_exec( "mysql -u $dbuser --password='$dbpassword' --host='$sqlhost' $dbname < $file 2>&1 1> /dev/null" );
$lines = explode( PHP_EOL, $output );
$errors = array();
foreach( $lines as $line )
{
if ( strtolower( substr( $line, 0, 5 ) ) == 'error' )
{
$errors[] = $line;
}
}
if ( count( $errors ) )
{
echo PHP_EOL . 'Errors occurred during import.';
echo implode( PHP_EOL, $errors );
}
else
{
echo 'No Errors' . PHP_EOL;
}
When issuing a exec, the shell will return a 0 on succes, or a number indicating a failure.
$result = exec( $command, $output );
should do the trick. Check result and handle appropiate.
You have done everything but look at the PHP manual! There is an additional parameter for the exec to return a result
http://php.net/manual/en/function.exec.php
"If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable."
exec($command,$output,$result);
if ($result === 0) {
// success
} else {
// failure
}

php shell_exec with realtime updating

I have this shell program that I want to execute by php. The problem is that it can potentially take a long time, and as of that I need it to have real-time updating to the user's browser.
I read that I may need to use popen() to do that, but I am sort of (ok, I really am :P) a PHP noob and can't figure out how I may be able to do it.
Would appreciate any help!
if( ($fp = popen("your command", "r")) ) {
while( !feof($fp) ){
echo fread($fp, 1024);
flush(); // you have to flush buffer
}
fclose($fp);
}
there is a dirty easy option
`yourcommand 1>&2`;
redirecting the stdout to the stderr.
there are two possible behaviors:
Non Block, where you need to do something else between flushs (#GameBit show how to do it).
With Block, where you wait until the called command finish, in this case look passthru function
I used this solution. It works fine for me.
$commandString = "myexe";
# Uncomment this line if you want to execute the command in background on Windows
# $commandString = "start /b $commandString";
$exec = popen($commandString, "r");
# echo "Async Code Test";
while($output = fgets($exec, 2048))
{
echo "$output <br>\n";
ob_flush();
flush();
}
pclose($exec);
try this code (tested on Windows machine + wamp server)
header('Content-Encoding: none;');
set_time_limit(0);
$handle = popen("<<< Your Shell Command >>>", "r");
if (ob_get_level() == 0)
ob_start();
while(!feof($handle)) {
$buffer = fgets($handle);
$buffer = trim(htmlspecialchars($buffer));
echo $buffer . "<br />";
echo str_pad('', 4096);
ob_flush();
flush();
sleep(1);
}
pclose($handle);
ob_end_flush();

Categories