Cant run shell script in PHP

2019-09-14 18:33发布

When I am trying to run shell script with exec and shell_exe nothing happens! When I run with those command ls or whoami all work's.

What could it be?

4条回答
Ridiculous、
2楼-- · 2019-09-14 19:18

If you're using Apache, check to make sure that the Apache user has the required permissions to execute the php file.

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-14 19:19

Do you echo the output?

echo exec('ls');

Do you have safe_mode enabled?

phpinfo();

When yes: (from the manual)

Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.

Try to call exec with

exec('...pathtoyourbashscript...', $out, $return);

Then

echo $return;

If it shows 127 it´s likely that the path is wrong.

Also check the permissions. User 'nobody' is probably the apache user, which needs the rights to access and execute the script.

You can change the permissions by running

chmod 755 pathtouyourscript

This means something like 'I don't mind if other people read or run this file, but only I should be able to modify it'.

查看更多
时光不老,我们不散
4楼-- · 2019-09-14 19:20

If the program is web based i.e. for linux, Try making a php file to process the shell. and a shell file to handle the php..

For instance: runAllShell.php file can contain a loop.:

<?php
// Developed by A T.
// Count the files
$directory = '/put/directory/path_here/';
$files = glob($directory . '*.*');

if ( $files !== false )
{
    $filecounter = count( $files );

}
else
{
    echo "No Files were found";
}
$fileCount = $filecounter;

// Start the index
$fileNumber = 1;
while($fileNumber <= fileCount){
shell_exec('$fileNumber . ".sh"');
// get some feedback
echo "The ".$fileNumber." file has been excecuted";
// increment file number
$fileNumber++;

}
?>

make sure that all the .sh files in the directory are numericaly ordered for this to work i.e: 1.sh 2.sh 3.sh and so on.

Best regards, AT.

查看更多
ら.Afraid
5楼-- · 2019-09-14 19:21

You can use reflection to figure out if the function has been disabled with disable_functions.

$exec = new ReflectionFunction('exec');
print $exec->isDisabled() ? 'Disabled' : 'Enabled';
查看更多
登录 后发表回答