0

我正在使用 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);
4

3 回答 3

1

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

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

从命令获取 stderr 输出

而不是使用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 脚本的任何内容都可能将当前工作目录更改为其他目录。

于 2013-08-12T21:49:24.187 回答
1

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

于 2013-08-12T22:58:47.780 回答
0

老实说,我认为使用phpseclib(一个纯 PHP SSH 实现)而不是使用 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()
?>
于 2013-08-13T14:22:11.363 回答