php ssh2 sudo,使用phpseclib ssh2 exec()函数时如何设置标志?

我正在使用phpseclib并且需要制作一些php函数,这些函数可以让某人以编程方式ssh到他们的服务器并更改root密码,还可以更改可能忘记密码的用户的密码(因此必须以根).

我尝试使用libssh2,但发现它有点讨厌使用.我现在正在研究看起来更健壮的phpseclib.但当我尝试使用’su’命令时:

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被禁用直接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​​标志(两次).

我找了很久,找不到任何东西.真的很感谢你的帮助.对于这篇文章的长度也很抱歉. 🙂

干杯

解决方法:

如果sudo passwd root需要在phpseclib中尝试read()/ write().例如.

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]');

?>

看起来你在那里得到一些帮助,然后停止跟进?有人建议您需要在命令中添加新行.你试过吗?他们还建议在phpseclib支持论坛上发帖.对我来说似乎是一个很好的建议……来源:https://www.icode9.com/content-1-438051.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值