2012-09-08 91 views
0

我有一个包含su的php脚本。当我在shell中运行它时,它会询问root密码并运行良好。但现在我想为它制作一个界面。因为您知道php-apache中的所有命令都以www数据用户身份运行,并且此用户没有指定密码,或者它不在sudoers中。所以我不能使用“echo pass | sudo -S ...”,因为我不知道什么是www-data的密码,并且它不符合每个服务器为www-data设置密码或将其添加到sudoers + nopass , 是吗?以root身份运行命令通过php

你可以建议什么解决方案来解决这个问题?

我的命令之一:

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

看看[suexec的(http://httpd.apache.org/docs/2.2/suexec.html ) – Lix

回答

2

phpseclib应该是最好的选择库,因为它并不需要任何额外的扩展。

<?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'); 
?> 

另一个替代方案是libssh2,但它必须被单独编译和是出了名difficult设置/使用。

0

我最近发布了一个项目,允许PHP获得并与真正的Bash shell交互,你可以很容易地获得一个带有root的shell。在这里获得:https://github.com/merlinthemagic/MTS

下载您只需使用下面的代码后:

$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