PHP shell_exec ssh 连接
我知道这个问题以前已经以多种不同的方式被问过,但我仍然在绞尽脑汁地思考为什么我无法让它发挥作用。
首先,我设置了两台 SLES 服务器,它们是服务器 A 和服务器 A。服务器 B 都在一个小型专用网络上运行,该网络只能由专门的团队访问。
服务器 A 配置为 Web 服务器,运行 Apache、PHP、MYSQL 和 ssh,所有这些都运行正常。
服务器 B 用于运行一些琐碎的任务,并且还安装并激活了 ssh。
我已经在服务器 A 上创建了 rsa 密钥,并将其安装在服务器 B 上,当在命令行运行时,我会立即登录,无需输入密码。我已经对 root 和 root 重复了这个过程。服务器 A 上没有人帐户。
我已将此 PHP 页面添加到服务器 A,如下所示:
<?php
shell_exec('ssh [email protected] ./StartTest.sh');
header("Location: archive.php?page=home");
?>
但是当我运行它时,它不会创建我的文件夹。如果我从命令行运行它,它对两者都有效(我想两者都适用,我不记得我现在是否在 cli 上为无人帐户尝试过此操作) root 和 root 。无人帐户。我什至将无人帐户添加到根组中,但仍然没有乐趣。
我在这里错过了什么吗?我想做的就是通过 php & 从服务器 A 连接到服务器 B。 ssh 执行一个命令并重定向到网站上的另一页面。
由于我的扑热息痛库存所剩无几,任何帮助将不胜感激。
I know this question has been asked before in many different ways but I'm still scratching my head over why I can't get this to work.
Firstly I have two SLES servers setup, these are Server A & Server B which are both running on a small private network which is only accessed by a dedicated team.
Server A is configured as a web server which is running Apache, PHP, MYSQL and ssh all of which are running problem free.
Server B is used to run menial tasks with ssh also installed and activated.
I have created my rsa key on Server A and installed it on Server B which when run at the command line logs me in straight away with out asking for a password. I have repeated this process for both root & nobody accounts on Server A.
I have added this a PHP page to Server A which looks like:
<?php
shell_exec('ssh [email protected] ./StartTest.sh');
header("Location: archive.php?page=home");
?>
But when I run it it does not create my folder. If I run this from the command line it works for both (I think both, I can't recall if I did try this for the nobody account on the cli now) root & the nobody account. I even went as far as adding the nobody account to the root group but still no joy.
Have I missed some thing here. All I would like to do is connect from Server A to Server B via php & ssh to execute one command and redirect to a another page on the web site.
Any help would be graciously appreciated as my paracetamol stock is running low.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
George Cummins 所说的内置 SSH 支持是不存在的。它是 PHP 的扩展,默认情况下不包含在内。它必须单独编译,并且非常难以设置/使用。我的建议是使用 phpseclib,一个纯 PHP SSH 实现:
The built-in SSH support George Cummins speaks of is non-existent. It is an extension to PHP that's not included by default. It has to be compiled separately and is notoriously difficult to setup / use. My recommendation would be to use phpseclib, a pure PHP SSH implementation:
我知道我这个答案太晚了,但也许可以帮助某人:
要使用
shell_exec
和ssh
你需要将参数添加到ssh
> 这些因此该命令不会尝试创建 .ssh 文件夹,并且您会得到一个清晰的输出,没有
ssh
日志I know that I'm too late at this answer but maybe can help someone:
To use
shell_exec
andssh
you need to add as parameter tossh
theseSo the command doesn't try to create .ssh folder and you have a clear output without log of
ssh
您说“我已将其添加为 PHP 页面”,因此我假设您是通过 Web 服务器执行此脚本,而不是作为独立脚本执行。
因此,该脚本可能不会从您期望的目录运行。您应该使用绝对(而不是相对)路径来确保脚本找到 ssh 二进制文件和您的脚本:
您还需要确认 Web 服务器用户有权执行 ssh 和 StartTest.sh脚本。
You said "I have added this a PHP page", so I will assume that you are executing this script via your web server, rather than as a standalone script.
As such, the script may not be running from the directory you expect. You should use absolute (rather than relative) paths to ensure that the script finds the
ssh
binary and your script:You will also need to confirm that the webserver user had permissions to execute ssh and the StartTest.sh script.