dongrong1856 2012-10-22 08:20
浏览 102
已采纳

通过exec的PHP SSH连接在从命令行调用时有效,但不能通过apache调用

My case is similar to PHP shell_exec running a shellscript with ssh but not the same.

Situation: I exchanged ssh-keys between the 2 servers, switched to the www-data user and connecting to the 2nd server via SSH works without password.

Test 1: ssh user@10.8.0.6 Documents/run.sh list works fine when executed in shell

Test 2: Putting a simple PHP Exec in a PHP file works fine. It returns an Array and the Retval is 0

Test 3: Putting the exec into a "big" PHP script and calling it will result in Retval 255 (Fatal Error ?!?!?)

So at the moment I don't really understand why it isn't working. I tried to figure out more details about the retval 255 but didn't get far.

The difference must be somewhere in PHP5 vs PHP5 cli. But before I had to use OpenVPN it worked fine also via normal Apache call.

  • 写回答

1条回答 默认 最新

  • dslkchyv673627 2012-10-22 08:50
    关注

    Generic

    The first level of diagnosis for shell_exec problems is trying to get a more informative error by spawning the same command adding 2>&1, e.g. in your case

    $retval = shell_exec("/path/to/ssh ... 2>&1");
    

    and inspecting $retval.

    Update

    'Host key verification failed' means that the ~/.ssh/known_hosts for the user running Apache contains a different key. Check in the file both hostname and IP keys; in a pinch, delete both, log in as user www-data and reinstate the keys by connecting manually.

    It is also possible, if connecting with a hostname, that the IPs have changed due to DHCP or different VPN tunnels being up, and that is not the host you're looking for.

    In the case of SSH, it is possible to execute it with -vvv very violently verbose option, and then parse through the kilobytes of output searching for the source of the known_hosts file. It can also be useful to shell_exec diagnostic commands such as

    $ret = shell_exec('set');
    

    to see the value of HOME variable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块