我使用的是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标志(两次)。
我找的年龄并没有找到任何内容。非常感谢你的帮助。对不起,这篇文章的长度也是如此。 :)
干杯
乔
您可以共享您正在使用的库。我不得不谷歌phpseclib,然后发现有两个SSH实现。 – Till 2011-04-23 01:21:03
啊,对不起。我正在使用我认为是最新的phpseclib版本:v 1.53 – Joe 2011-04-23 10:15:58
不,我的意思是,phpseclib提到'Net_SSH'和'Net_SSH2'。你在用哪个? :) – Till 2011-04-23 17:03:55