使用 phpseclib ssh2 exec() 函数时如何设置标志? [英] How do i set flags when using phpseclib ssh2 exec() function?
问题描述
我正在使用 phpseclib 并且需要制作几个 php 函数,使某人能够以编程方式 ssh 进入他们的服务器并更改 root 密码,并更改可能忘记密码的用户的密码(因此必须以 root 身份登录).
I'm using phpseclib and need to make a couple of php functions that enable someone to programmatically ssh into their server and change the root password and also change the password of a user that may have forgotten their password (so have to be logged in as root).
我尝试使用 libssh2,但发现它使用起来有点讨厌.我现在正在研究 phpseclib,它看起来更健壮.但是当我尝试像这样使用su"命令时:
I tried using libssh2, but found it a bit nasty to use. I'm now looking at phpseclib which seems more robust. But when I tried to use the 'su' command like so:
echo $ssh->exec('su');
我收到回复:
su: must be run from a terminal
当我尝试使用 sudo 时:
and when I try to use sudo:
echo $ssh->exec('sudo passwd root');
我收到错误:
sudo: no tty present and no askpass program specified
无论如何,事实证明 su 被禁用以进行直接 ssh 访问,但是在查看了 这篇文章,原来你可以用下面的命令做到:
Anyway, it turns out that su is disabled for direct ssh access, but after having a look at this article, it turns out you can do it with the following command:
ssh -t -t -l 'username' 'host' 'su -'
无论如何,当从我的笔记本电脑(运行 ubuntu)进入终端时,这最终对我有用,然后我输入了我的密码,然后输入了 root 密码以完成.
That's what finally worked for me anyway when entering into a terminal from my laptop (running ubuntu), and then I entered my password and then the root password to finish off.
引用上面链接的网站:
当给出 -t 时,ssh 命令(使用 -t)远程 sshd 建立一个到工作进程的伪终端"管道.
Ssh commands (using -t) the remote sshd to establish a 'pseudo-terminal' pipe to the worker process when -t is given.
.只要 ssh 的标准输入是终端,ssh 就会这样做.
. ssh does this as long as its stdin is a terminal.
.但是如果ssh的stdin是非终端的,ssh不会引导sshd建立一个
除非给出两个 -t 的伪终端:
. 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:
回显密码 |ssh -t -t -l 用户名远程主机
echo password | ssh -t -t -l username remote_host
.因此,使用 -t -t(来自 ssh)sshd 为客户端进程设置了一个伪终端.
. So with -t -t (from ssh) sshd sets up a pseudo-terminal to the client process.
.客户端,无论是 'tty' 还是 'su' 都无法判断它连接到一个虚构的>终端:
. The client, whether it be 'tty' or 'su' cannot tell it is connected to a ficticious >terminal:
echo dummystr |ssh -t -t -l 用户名 host.com -c ''tty'
echo dummystr | ssh -t -t -l username host.com -c ''tty'
回显密码 |ssh -t -t -l 用户名 host.com -c 'su -'
echo password | ssh -t -t -l username host.com -c 'su -'
答案来了.如果您通过一个 > 交互式客户端 ssh(例如来自 OpenBSD 的客户端 ssh)在 linux 机器上su root",请使用 double -t.
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.
所以,正如我上面所说的那样,它实际上是在终端上工作的:
So, it actually worked from the terminal as I said above using:
ssh -t -t -l 'username' 'host' 'su -'
但我真的希望能够使用 phpseclib 执行此命令.唯一的问题是我不知道如何将任何标志放入 exec() 函数中.具体来说,我需要放入 -t 标志(两次).
but I really want to be able to execute this command using phpseclib. Only thing is I don't know how to put in any flags into the exec() function. Specifically, I need to put in the -t flags (twice).
我找了好久都没找到.非常感谢您对此提供的帮助.也很抱歉这篇文章的长度.:)
I've looked for ages and can't find anything. Be really grateful for some help on this. Sorry about the length of this post as well. :)
干杯
乔
推荐答案
可以通过调用启用伪终端
You can enable the pseudoterminal by calling
$ssh->enablePTY();
在您登录之后,但在 exec 之前.这将防止它抱怨缺少 tty.
after you login, but before the exec. This will prevent it from complaining about the missing tty.
这篇关于使用 phpseclib ssh2 exec() 函数时如何设置标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!