shell_exec 在 Ajax 调用导致无限循环

shell_exec in Ajax call causing infinite loop

我有这个 Ajax 电话:

$('#directory_select_form').submit(function(e) {
        $('#loader').show();
        $('#order_number_submit_btn').prop('disabled', true);
        e.preventDefault();
       $.ajax({
           url: 'includes/confirmation_emails/process_directory_request.php',
           type: 'POST',
           dataType: 'json',
           data: $(this).serialize(),
           success: function(data) {
               console.log(data);
               $('#loader').hide();
               $('#order_number_submit_btn').prop('disabled', false);
           },
           error: function(jqXHR, textStatus, errorThrown) {
               $('#loader').hide();
               $('#order_number_submit_btn').prop('disabled', false);
           }
       })
    });

process_directory_request.php 包含一个 shell_exec 命令:

<?php

$selected_directory = $_POST['selected']; //<-- This will be used later. Hard coded for now
$order_number = $_POST['orderId']; //<-- This will be used later. Hard coded for now.


$command = shell_exec('ssh remoteServer grep -o "Order Number 1234567" /mnt/tank/TECH/"MySQL\ Data\ Archives"/path/to/the/file');

echo json_encode($command);

它正在崩溃,因为由于某种原因它进入了无限循环。如果我 运行 来自命令行的命令,它会按预期执行。

我不能使用 this answer 因为开头是 ssh。我正在远程 运行ning 命令。

是什么导致了这个无限循环?我不明白。我正在将 ajax 响应数据打印到控制台,它一遍又一遍地显示同一行。

编辑 这是我的开发控制台中的输出,但实际上输出了很多次:

/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order
/mnt/tank/TECH/MySQL Data Archives/path/to/the/file:Order

这不是无限循环,它只是比您预期的输出更多。

问题是报价没有被发送到远程服务器。 ssh 简单地将所有参数连接成一个命令,所以远程命令是:

grep -o Order Number 1234567 /mnt/tank/TECH/MySQL\ Data\ Archives/path/to/the/file

它只是搜索单词 Order,并将 Number1234567 视为要搜索的文件。

您需要对双引号进行转义,以便按字面意思发送。

$command = shell_exec('ssh remoteServer grep -o \"Order Number 1234567\" /mnt/tank/TECH/\"MySQL Data Archives\"/path/to/the/file');

此外,您不需要在带空格的目录名称中同时使用双引号和转义空格。

Barmar 正确地指出问题出在您的未转义值上。这是我解决问题的方法:

<?php

$selected_directory = escapeshellarg("/mnt/tank/TECH/MySQL Data Archives/$_POST[selected]");
$order_number = escapeshellarg("Order Number $_POST[orderId]");

$grep_cmd = escapeshellarg("grep -o $order_number $selected_directory");

$output = shell_exec("ssh remoteServer $grep_cmd);

header("Content-Type: application/json");
echo json_encode($output);

我们对参数进行双重转义,以便它们毫发无损地通过 SSH 调用。