2016-07-26 10 views
1

phpseclibを使用してphpからsshコマンドを呼び出します。すべて以下の例ではphpseclibを使用してubuntuコマンドをroot権限で実行する方法

$ip = 'ip_address'; 
$login = 'user'; 
$password = 'password'; 
$ssh = new SSH2($ip); 
$ssh->login($login, $password); 

okです(私は許可が必要といけないので):私はsudoの秀を使ってログインしようとした

$command = 'ip addr flush dev eth1'; 
$result = $ssh->exec($command); // result = 'Failed to send flush request: Operation not permitted' 

$command = 'ifconfig'; 
$result = $ssh->exec($command); // result = 'eth0 ..., eth1 etc.' 

問題は、次の例で発生:

$result = $ssh->read('$'); // result = '..... [email protected]:~$' 
$result = $ssh->write("sudo su\n"); //result = true 
$result = $ssh->read(':'); // result = 'sudo su\n [sudo] password for user:' 
$result = $ssh->write($password."\n"); // result = true 
$result = $ssh->read('#'); //result = '[email protected]:/home/user# 

帽子はすべて大丈夫です(私は根元ですが):

$command = 'ip addr flush dev eth1'; 
$result = $ssh->exec($command); // result = 'Failed to send flush request: Operation not permitted' 

問題はどこですか?前もって感謝します!

+0

自分自身をsudoersファイルに追加してから、sudoの後にコマンドを入力するだけです(パスワードを入力する必要はありません)。 –

+0

suはすでにsuidバイナリです。 rootとして実行されるので、 'sudo'を実行する必要はありません。 –

+0

私は端末でこれを試しました: 'adduser user sudo'と結果は 'ユーザ 'はすでに' sudo 'のメンバーです。次に、私は端末に 'su'だけを使用し、結果は' Password: su:Authentication failure'です。 – Krzychu

答えて

1

私はhttp://mrbluecoat.blogspot.com/2014/03/running-sudo-commands-with-phpseclib.htmlから次のコードを使用:

$ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); 
$ssh->write("sudo YOUR COMMAND HERE\n"); 
$ssh->setTimeout(10); 
$output = $ssh->read('/.*@.*[$|#]|.*[pP]assword.*/', NET_SSH2_READ_REGEX); 
if (preg_match('/.*[pP]assword.*/', $output)) { 
    $ssh->write($sudo_password."\n"); 
    $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); 
} 

をし、それが動作します!

+0

'$ ssh-> setTimeout(10)'を実行する必要はありません。 – neubert

0

rootユーザーグループにhttpd process(apache、php)を追加する必要があります。補助プロセスはroot permで実行できます。

関連する問題