【问题标题】:Ajax request to PHP page and exec(ssh....) not working对 PHP 页面的 Ajax 请求和 exec(ssh ....) 不起作用
【发布时间】:2013-08-14 08:41:50
【问题描述】:

我正在使用 ajax 发布到 php 页面(忽略发布的数据,这不重要)

当我使用以下命令在我的 linux 服务器上运行 php 页面时:php addHit.php 它正确地回显了远程服务器的主机名。然而这在 ajax 中不会发生,我得到的只是成功函数所在的空白警报。你可以在这里看到它的实际效果:http://ec2-54-244-169-118.us-west-2.compute.amazonaws.com/bootstrap/jumbotron-narrow/index.php

    <script>
        $(function() {  
            $("form[name=addHit]").submit(function() {  
                alert("I am an alert box!");
                var link = $("input[name=link]").val();
                var comments = $("input[name=comments]").val();
                var datastring = "link="+link+"&comments="+comments;
                alert(datastring);
                $.ajax({
                    type: "POST",  
                    url: "/bootstrap/jumbotron-narrow/addHit.php",  
                    data: datastring,  
                    success: function(data, status, xhr) {  
                        alert(data);
                    }, 
                    error: function(httpRequest, textStatus, errorThrown) { 
                       alert("status=" + textStatus + ",error=" + errorThrown);
                    }
                });  
                alert("here");
                return false;
            }); 
        });  
    </script>

我的 addHit.php 页面

$commands = "ssh -i adoekey.pem ubuntu@ip-10-250-69-130.us-west-2.compute.internal hostname -f ";
echo exec($commands);

【问题讨论】:

  • 你确定ssh 在任何shell PHP 使用的路径中吗? adoekey.pem 文件是否与 php 脚本等在同一目录中?你可以做很多事情来自己调试。
  • pem 文件与 php 脚本位于同一目录中,我假设由于运行 php addHit.php 给了我正确的结果,ajax 也会。
  • 错误的假设。 php-in-webserver 是一个与 php-in-your-personal-shell-prompt 非常不同的环境。

标签: php jquery ajax ssh exec


【解决方案1】:

老实说,我认为使用phpseclib, a pure PHP SSH implementation 会更容易,而不是使用proc_open。例如

<?php
include('Net/SSH2.php');
include('Crypt/RSA.php');

$ssh = new Net_SSH2('ip-10-250-69-130.us-west-2.compute.internal');
$key = new Crypt_RSA();
$key->loadKey(file_get_contents('adoekey.pem'));
if (!$ssh->login('ubuntu', $key)) {
    exit('Login Failed');
}

//stderr will be included in output unless specifically disabled
//$ssh->enableQuietMode();
echo $ssh->exec('hostname -f');
//be quiet mode enabled or not you can still get stderr with $ssh->getStdError()
?>

【讨论】:

    【解决方案2】:

    @Archetype2 如何解决问题(来自他的帖子):

    我必须创建文件夹 /var/www/.ssh 并将 /root/.ssh 文件夹中的项目复制到这个新文件夹中,并将新目录及其内容的所有权更改为 www-data。然后我把pem文件的权限改成了400。

    从命令中获取标准错误输出

    不要使用exec 来运行命令,而是使用以下命令(来自“PHP StdErr after Exec()”):

    $descriptorspec = array(
        0 => array("pipe", "r"),  // stdin
        1 => array("pipe", "w"),  // stdout
        2 => array("pipe", "w"),  // stderr
    );
    
    $command = "ssh -i adoekey.pem ubuntu@ip-10-250-69-130.us-west-2.compute.internal hostname -f ";
    $pipes = '';
    $process = proc_open($command, $descriptorspec, $pipes, dirname(__FILE__), null);
    
    $stdout = stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    
    $stderr = stream_get_contents($pipes[2]);
    fclose($pipes[2]);
    
    echo "stdout : \n";
    var_dump($stdout);
    
    echo "stderr :\n";
    var_dump($stderr);
    
    $returnCode = proc_close($process);
    echo "Return code: " . $returnCode;
    

    当您运行php addHit.php 命令时,您是以您登录的用户身份运行它(可能是root?)。 HTTP 服务器很可能拥有自己的用户,但权限受到严格限制。你的服务器配置是什么?您正在运行 LAMP 堆栈吗?

    还可以尝试使用.pem 文件的绝对文件路径,因为执行您的 php 脚本的任何内容都可能将当前工作目录更改为其他内容。

    【讨论】:

    • 我认为你正在做某事,从 root 用户转到 ubuntu,我运行了 php addHit.php,它给了我权限被拒绝(公钥)。
    • 当您运行 PHP 的 exec 命令时,它不会捕获 stderr(打印错误的位置),因此这可能是 exec 命令没有输出的原因。
    • 所以 apache 用户是 www-data,我确实将 php 和 pem 文件 chown 给用户 www-data,创建了绝对文件路径,但它仍然无法正常工作....我需要看到一些错误日志,我该如何记录?
    • 在相关的说明中,您可能应该使用 SSH 库来简化您的操作。不推荐通过 PHP 在宿主系统上运行命令。
    • 非常有用!它给了我一些错误,我设法调试它们,现在它正在工作,谢谢!
    【解决方案3】:

    我必须创建文件夹 /var/www/.ssh 并将 /root/.ssh 文件夹中的项目复制到这个新文件夹中,并将新目录及其内容的所有权更改为 www-data。然后我把pem文件的权限改成了400。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-19
      • 2018-07-04
      • 2017-01-11
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      相关资源
      最近更新 更多