php - Ajax 调用中的 shell_exec 导致无限循环
问题描述
我有这个 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);
它崩溃了,因为由于某种原因它进入了无限循环。如果我从命令行运行命令,它会按预期运行。
因为一开始我不能使用这个答案ssh
。我正在远程运行命令。
是什么导致了这个无限循环?我不明白。我正在将 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
,并将Number
和1234567
视为要搜索的文件。
您需要转义双引号,以便按字面意思发送它们。
$command = shell_exec('ssh remoteServer grep -o \\"Order Number 1234567\\" /mnt/tank/TECH/\\"MySQL Data Archives\\"/path/to/the/file');
此外,您不需要在带有空格的目录名称中同时使用双引号和转义空格。
推荐阅读
- c# - 错误 CS0246:找不到类型或命名空间名称“PHFetchOptions”(您是否缺少 using 指令或程序集引用?
- javascript - mapStateToProps 在最顶部的元素中导致奇怪的警告?
- xamarin - 错误 CS0534:[java 类] 未实现继承的抽象成员 'AsyncTask.DoInBackground(params Object[])'
- python - 嵌入python时Applescript输出读取问题
- html - Index.html 为空并显示 0bytes 但网页正在加载
- google-cloud-firestore - 如何允许身份验证部分下具有特定用户 UID 的用户写入 Firestore 并让其他用户读取特定文档的数据?
- python - 在 for 循环结束后不保留 Pandas DataFrames 中的更改
- pandas - 使用 panda 读取 .csv 并将单个列输出到单独的 .csv 文件中
- python - 如何在 selenium python 测试中设置等待条件
- json - 如何在laravel的特定字段下显示json响应?