PHP - How to get Shell errors echoed out to screen
The error data is output from the target program's STDERR
stream. You can get access to the error data through the normal returned string from shell_exec()
by appending 2>&1
to the command, which will redirect STDERR
to STDOUT
, the stream that you are currently seeing:
var_dump(shell_exec("ffmpeg -i /var/www/html/sitedomain/httpdocs/tmp/ebev1177.mp4 2>&1"));
You may also want to take a look at proc_open()
which will allow you to get access to STDIN
, STDOUT
and STDERR
as three individual streams, which can afford much finer grained control over the target program and exactly how you handle the input and output to it, including redirecting any and all of them directly to a log file if so desired. Be aware though that this is a much more complex mechanism with many pitfalls and tripping hazards.
More information on the standard streams can be found here.
php run command line and pipe std err to variable
ok I was able to find a solution. You need to set up a native process to configure the pipes.
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a file to write to
);
$cwd = '/tmp';
$env = array('some_option' => 'aeiou');
$process = proc_open('ls -lasdfsdfwefwekj', $descriptorspec, $pipes, $cwd, $env);
/*fwrite($pipes[0], '<?php print_r($_ENV); ?>');
*/
fclose($pipes[0]);
echo "stdout=".stream_get_contents($pipes[1]);
echo "stderr=".stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = proc_close($process);
echo "command returned $return_value\n";
php shell_exec command doesn't give an outpu of the python scriptt in ubuntu
Try to catch error stream by appending
2>&1
to the command:$output = shell_exec("python3 $chk $sym 2>&1");
See PHP - How to get Shell errors echoed out to screenYou potentially have an OS command injection vulnerability (you concatenate user input with a shell command). See https://portswigger.net/web-security/os-command-injection
Shell output not being fully retrieved by PHP!
You read only the first 4,096 bytes from the pipe, you'll need to place the fread
/print
_r in a loop and check for the end-of-file using the feof
function.
$handle = popen('python last', 'r');
while(!feof($handle))
{
print_r(fread($handle, 4096));
}
pclose($handle);
Can I try/catch a warning?
Set and restore error handler
One possibility is to set your own error handler before the call and restore the previous error handler later with restore_error_handler()
.
set_error_handler(function() { /* ignore errors */ });
dns_get_record();
restore_error_handler();
You could build on this idea and write a re-usable error handler that logs the errors for you.
set_error_handler([$logger, 'onSilencedError']);
dns_get_record();
restore_error_handler();
Turning errors into exceptions
You can use set_error_handler()
and the ErrorException
class to turn all php errors into exceptions.
set_error_handler(function($errno, $errstr, $errfile, $errline) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
try {
dns_get_record();
} catch (ErrorException $e) {
// ...
}
The important thing to note when using your own error handler is that it will bypass the error_reporting
setting and pass all errors (notices, warnings, etc.) to your error handler. You can set a second argument on set_error_handler()
to define which error types you want to receive, or access the current setting using ... = error_reporting()
inside the error handler.
Suppressing the warning
Another possibility is to suppress the call with the @ operator and check the return value of dns_get_record()
afterwards. But I'd advise against this as errors/warnings are triggered to be handled, not to be suppressed.
PHP shell_exec() - not printing dynamic output, only prints static echo text
Is wanpipemon
in the path of whatever shell PHP's using to execute the script? The 'file not found' type errors would be written to stderr and not trapped by your string building in the script, and also not caught by PHP unless you did stderr redirection:
$output = shell_exec('/usr/sbin/linesta.sh 2>&1');
^^^^--- redirect stderr to stdout.
Related Topics
How to Use a Like Clause in a Pdo Prepared Statement
Symfony 3 - an Exception Occurred in Driver: Could Not Find Driver
PHP - Syntax Error, Unexpected T_Constant_Encapsed_String
Mongodb Get _Id as String in Find Query
Using PHP Code in Smarty Tpl File
How to Execute MySQL Command Delimiter
Php:File_Get_Contents($Loc) Fails
Convert a Big Integer to a Full String in PHP
How to Parse HTML Table Using PHP
In Htaccess, I'D Like to Replace Underscores with Hyphens and Then Redirect the User the New Url
Class Firebase\Jwt\Jwt Not Found
Php: How to Create Unicode Filenames
Laravel Form Methods VS Traditional Coding
Php, Why Do You Escape My Quotes
Insert Value List Does Not Match Column List: 1136 Column Count Doesn't Match Value Count
SQL Server Error 1934 Occurs on Insert to Table with Computed Column PHP/Pdo