2012-09-08 90 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