分享

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

 印度阿三17 2019-09-03

我正在使用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().例如.

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

来源:https://www./content-1-438051.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多