2011-04-20 57 views
3

我使用的是phpseclib,需要制作一些PHP函数,让某人以编程方式ssh进入他们的服务器并更改root密码,并更改可能忘记密码的用户的密码(所以必须记录以root身份)。如何在使用phpseclib ssh2 exec()函数时设置标志?

我试过使用libssh2,但发现它有点讨厌使用。我现在看着看起来更健壮的phpseclib。但是,当我试图用 '苏' 命令,像这样:

echo $ssh->exec('su'); 

我得到的回复:

su: must be run from a terminal 

,当我尝试使用sudo:

echo $ssh->exec('sudo passwd root'); 

我得到错误:

sudo: no tty present and no askpass program specified 

无论如何,事实证明,su是d isabled直接ssh访问,但在看看this article后,事实证明,你可以用下面的命令做到这一点:

ssh -t -t -l 'username' 'host' 'su -' 

那进入我的笔记本电脑终端时,就是终于为我工作反正(运行Ubuntu的),然后我输入我的密码,然后输入root密码完成。

从链接到网站上面引述:正如我上面用说

Ssh commands (using -t) the remote sshd to establish a 'pseudo-terminal' pipe to the worker process when -t is given.

. ssh does this as long as its stdin is a terminal.

. But if ssh's stdin is a non-terminal, ssh won't direct sshd to establish a
pseudo-terminal unless TWO -t's are given:

echo password | ssh -t -t -l username remote_host

. So with -t -t (from ssh) sshd sets up a pseudo-terminal to the client process.

. The client, whether it be 'tty' or 'su' cannot tell it is connected to a ficticious >terminal:

echo dummystr | ssh -t -t -l username host.com -c ''tty'

echo password | ssh -t -t -l username host.com -c 'su -'

So there is the answer. Use double -t if you are 'su root'ing' on a linux box through an >interactive client ssh like the one from OpenBSD.

因此,它实际上是从终端的工作:

ssh -t -t -l 'username' 'host' 'su -' 

,但我真的希望能够执行此命令使用phpseclib。唯一的问题是我不知道如何将任何标志放入exec()函数中。具体来说,我需要输入-t标志(两次)。

我找的年龄并没有找到任何内容。非常感谢你的帮助。对不起,这篇文章的长度也是如此。 :)

干杯

+0

您可以共享您正在使用的库。我不得不谷歌phpseclib,然后发现有两个SSH实现。 – Till 2011-04-23 01:21:03

+0

啊,对不起。我正在使用我认为是最新的phpseclib版本:v 1.53 – Joe 2011-04-23 10:15:58

+0

不,我的意思是,phpseclib提到'Net_SSH'和'Net_SSH2'。你在用哪个? :) – Till 2011-04-23 17:03:55

回答

1

我看着phpseclib的Net_SSH2暂时。它看起来像使用套接字连接到服务器。所以没有办法通过-t两次。这不是一个ssh电话。

既然你在你的问题中提到了libssh2,有一个支持它的PEAR包装器,可能会让事情变得更容易,代码目前只在SVN中。 PEAR包装也称为Net_SSH2,但与phpseclib的Net_SSH2(混淆)不同。

退房代码在这里:

下载它,与做一个svn签出:

svn co http://svn.php.net/repository/pear/packages/Net_SSH2/trunk/ ./Net_SSH2 

小例子:

<?php 
require_once './Net_SSH2/Net/SSH.php'; 
$ssh = new Net_SSH2::factory('LibSSH2', array(
    'login_name' => 'user', 
    'password' => 'pass', 
    'hostname' => 'example.org', 
    'command' => 'su -', 
)); 
$ssh->sshExec($std_output, $std_error); 
var_dump($std_output, $std_error); 

会有帮助吗?

+0

谢谢直到。还没有时间检查一下,但我会。我完成后会回复。 – Joe 2011-04-25 09:56:42

2

如果sudo passwd root需要tty尝试在phpseclib中读取()/ write()。例如。

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

$ssh = new Net_SSH2('localhost', 22); 
$ssh->login('username', 'password'); 

$ssh->read('[prompt]'); 
$ssh->write("sudo passwd root\n"); 
$ssh->read('Password:'); 
$ssh->write("Password\n"); 
echo $ssh->read('[prompt]'); 
?> 

其实,我只是从您的其他职务复制/粘贴:php ssh2_exec not executing 'su' command

看起来你在那里得到一些帮助,然后停止跟进?有人建议你需要添加新行到你的命令。你尝试过吗?他们还建议在phpseclib支持论坛发帖。好像建议对我好一点......

+1

我不明白直到哪个例子都会起作用。 su - 提示输入密码不是吗?在这个例子中,哪里是提供给su的密码?有SSH的密码进入服务器,但不是密码做的... 此外,当我做了谷歌搜索它,我看到net_SSH1和Net_SSH2 phpseclib。不是Net_SSH和Net_SSH2。我认为这很重要,因为有两种不同的SSH协议 - SSH1和SSH2。看起来像一个类会对应一个,另一个类对应另一个类... – rhinestone 2011-04-24 00:22:05

+0

取决于 - 我想你可以使用'sudo'来代替。这可能是更好的选择。 – Till 2011-04-25 21:25:24

1

你可以通过调用

$ssh->enablePTY(); 

你登录后启用伪终端,但Exec之前。这将防止它抱怨缺少的tty。