2013-08-01 178 views
0

我在過去的3天中遇到了一個問題。還找不到解決方案。phpseclib無法遠程執行命令

我有一個Cent OS遠程機器和一個Ubuntu本地機器。我在本地機器上有一個名爲test.php的php腳本,以便我想使用該php腳本在遠程計算機上運行一些Linux命令。我使用phpseclib連接到遠程機器。以下是php腳本test.php

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

define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX); 

$ssh = new Net_SSH2('10.3.2.0'); 
if (!$ssh->login('makesubdomain','abcdabcd')) { 
    exit('Login Failed'); 
} 
echo $ssh->exec('/usr/local/listdomain.backup/test/makedir.sh'); 
?> 

自根登錄已在遠程百分之OS機被停用我不能在此使用root用戶。 所以我創造了這個makesubdomain用戶,並給須藤權限,太沒有由下一個在/etc/sudoers文件。該添加makesubdomain ALL=(ALL) NOPASSWD: ALL密碼是shell腳本駐留在10.3.2.0

sudo -H sh -c ' 
    mkdir /usr/local/testdir 
    if [ $? -eq 0 ];then 
     echo "success"; 
    else 
     echo "not success"; 
    fi 
' 

但現在當我運行從PHP腳本終端使用命令php test.php它顯示錯誤sudo: sorry, you must have a tty to run sudo。最終,我需要做什麼與test.phpmakedir.sh創建一個目錄testdir在.sh文件中指定使用給定的PHP腳本與用戶makesubdomain。請教,因爲我是一個非常初學者的PHP。

(注:我可以爲用戶makesubdomain在遠程計算機上成功運行makedir.sh文件,用命令sudo ./makedir,那太不提示輸入sudo密碼)

編輯

我曾在評論Defaults requiretty/etc/sudoershttp://www.unix.com/shell-programming-scripting/201211-sudo-sorry-you-must-have-tty-run-sudo.html中給出,它工作正常。如果不這樣做,我可以有其他選擇嗎?

回答

0

嘗試在做$ssh->exec()之前致電$ssh->enablePTY()