简体   繁体   中英

run command as root via php

I have a php script which contains su. when I run it in shell it ask for root password and works well. but now I want make an interface for it. as you know all command in php-apache runs as www-data user and this user doesn't have specified password or it's not in sudoers. so I can't use "echo pass | sudo -S ..." because I don't know what is www-data's password and It is not logical on every server set password for www-data or add it in sudoers+nopass, is it?

What solution can you suggest to solve this problem?

one of my commands:

su -c \"/usr/sbin/dmidecode -s baseboard-serial-number | tr \  \-\"

phpseclib should be best library choice as it does not requires any additional extensions.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('example.com');
if (!$ssh->login('user', 'pass')) {
    exit('Login Failed');
}

echo $ssh->exec('whoami');
echo $ssh->exec('pwd');
?>

Another alternative is libssh2 , but it has to be compiled separately and is notoriously difficult to setup/use.

I recently published a project that allows PHP to obtain and interact with a real Bash shell, you can easily get a shell with root. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);

$strCmd = "\"/usr/sbin/dmidecode -s baseboard-serial-number | tr \  \-\"";
$return1  = $shell->exeCmd($strCmd);

echo $return1;// return from your command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM