PHP shell_exec 使用 ssh 运行 shell 脚本
我有一个 shellscript,它通过 ssh 和密钥连接到另一台机器,因此它不需要用户名和密码。
当我从命令行运行这个脚本时,它工作正常..但是当我从 php shell_exec 运行这个脚本时,它不起作用。
如果我与 PHP 建立 ssh 连接并以我自己的用户身份运行脚本,它确实可以工作。
现在回答我的问题:D 有没有办法只从 php 运行 shell_exec 中的脚本,而无需以其他用户身份通过 ssh 建立连接?
I have a shellscript with connects to a a different machine with ssh and a key so it does not need the username and password.
When i run this script from commandline it works fine.. but when I run this script from php shell_exec it does not work.
If I make an ssh connection with PHP and run the script as my own user it does work.
Now for my question :D
Is there a way to just running the script in shell_exec
from php without making an connection over ssh as a different user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否正确指定了私钥文件?
Did you specify the private key file correctly?
如果您使用的是 Ubuntu 或 Debian,则 Web 服务器将以用户名 www-data 运行。对于其他系统,请检查 Web 服务器配置中的用户名。您可以简单地测试该用户(以及您的 php Web 应用程序)是否能够进行 SSH 连接。
1) 成为 Web 服务器的用户
2) 尝试连接远程主机
如果您无需输入密码即可连接,则一定存在不同的问题。如果您必须输入密码,则密钥文件是为其他用户存储的 - 而不是 www-data。您已将 SSH 配置为使用密钥。对您的本地用户 www-data 执行相同的操作,它将起作用。
If you are using Ubuntu or Debian the web server is running with the user name www-data. For other systems please check the web server configuration for the user name. You can simply test if this user (and your php web application) is able to do the SSH connection.
1) Become the user of your web server
2) Try connecting the remote host
If you will get connected without entering a password there must be a different problem. If you have to enter a password, the key files were stored for a different user - not for www-data. You have already configured SSH to use the key. Do the same for your local user www-data and it will work.
看来 ssh 连接不适用于
shell_exec
。如果我在ssh2_exec
下运行shellscript
它似乎确实有效。这有点奇怪,因为 ssh 连接是在脚本文件中使用公钥和私钥建立的。我认为这只会运行:s
允许网络服务器执行该文件,因为那里还有其他命令可以工作正如预期的那样。
It seems ssh connection does not work with
shell_exec
. If i run theshellscript
underssh2_exec
it does seem to work.Which is a little strange as the ssh connection is made in the script file with a public and private key.. I would assume this would just run :s
The webserver is allowed to execute the file, as there are other command in there who work as expected.