JeanCremers Posted July 14, 2020 Share Posted July 14, 2020 (edited) Hello, I can't get exec() working with a binary, but it works with whoami, the binary also outputs on a plain linux machine. I have error reporting E_ALL enabled but i can't find anything usefull in the logs. What can i do? Thanks for helping. See pic for the output of the script, whoami works but swetest gives nothing. I know it works, it is in use elsewhere on servers. <?php if (ini_get('safe_mode')) echo 'safe mode is on<br>'; else echo 'safe mode is off<br>'; if (function_exists('exec')) echo "exec is enabled<br>"; else echo "exec is disabled<br>"; echo exec('whoami'); echo exec('swetest -h'); ?> Edited July 14, 2020 by JeanCremers Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/ Share on other sites More sharing options...
kicken Posted July 14, 2020 Share Posted July 14, 2020 If you look at the manual page for exec you'll see that it returns just Quote The last line from the result of the command. If you look at the arguments list, you'll see there are two additional arguments you can pass to exec Quote output If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec(). return_var 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. So if you want the full output, you need to pass a variable as the second argument and it will be filled with the output. exec('swetest -h', $output); var_dump($output); Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/#findComment-1579626 Share on other sites More sharing options...
JeanCremers Posted July 14, 2020 Author Share Posted July 14, 2020 Sorry, yes, you're right, i tried that however. I get array(0) { } Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/#findComment-1579627 Share on other sites More sharing options...
kicken Posted July 14, 2020 Share Posted July 14, 2020 Then you need to check a few things. Use the third parameter to get the exit-code and see what it is. Typically 0 would mean success, anything else would be an error. There are two ways a program can output text, either via STDOUT, or STDERR. By default only STDOUT is captured. To capture STDERR as well you need to redirect it to STDOUT. This is done by adding 2>&1 to the end of your command. It's generally best to use a full path to the executable as your script may be running with a different $PATH value than you're normal shell. Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/#findComment-1579629 Share on other sites More sharing options...
JeanCremers Posted July 14, 2020 Author Share Posted July 14, 2020 (edited) Thanks! I get error 127, path not found indeed. I tried, exec('swetest'); exec('./swetest'); exec('/public_html/se/swetest'); Neither works. ps. i see google is full of exec error 127 If i dump it all in the server root i still get 127 so something must be fishy in the path? Edited July 14, 2020 by JeanCremers Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/#findComment-1579631 Share on other sites More sharing options...
JeanCremers Posted July 15, 2020 Author Share Posted July 15, 2020 Tried this, again got an empty array and error code 126, 'command not executable', i'm suspecting this is some rights issue. <?php $path = realpath('swetest'); exec($path . ' -h', $out, $res); var_dump($out); echo $res; ?> Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/#findComment-1579641 Share on other sites More sharing options...
JeanCremers Posted July 17, 2020 Author Share Posted July 17, 2020 This was caused by a 32 bit executable on 64 bit os, provider has made a fix for me. Quote Link to comment https://forums.phpfreaks.com/topic/311084-exec-does-not-always-show-output/#findComment-1579744 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.