打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用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().例如.

<?phpinclude('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支持论坛上发帖.对我来说似乎是一个很好的建议……

来源:https://www.icode9.com/content-1-438051.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Docker入门系列6 如何打开多个终端进入Docker容器
Linux su和sudo命令有什么区别?
mac terminal ssh client shuttle 免输密码
SVN入门及配置使用
如何在Ubuntu 20.04上启用SSH
动手实现Linux远程控制
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服