简体   繁体   English

将参数从PHP传递到远程Shell脚本

[英]Passing arguments from PHP to a remote shell script

I am executing a shell script located on a remote machine from a PHP script. 我正在从PHP脚本执行位于远程计算机上的Shell脚本。 So, let's say that PHP script runs on A and shell script runs on B (10.0.0.37). 因此,假设PHP脚本在A上运行,而shell脚本在B(10.0.0.37)上运行。

I have the following code which runs well 我有以下运行良好的代码

$cmd = "ssh 10.0.0.37 /usr/tmp/script.sh";
exec($cmd, $output);

Now, I want to pass arguments to the shell script, preferably in JSON format. 现在,我想将参数传递给shell脚本,最好采用JSON格式。

The output of echo json_encode($arg) is as follows: echo json_encode($arg)的输出如下:

[{"original_name":"pdf_convert","changed_name":"pdf_convert_1"},{"original_name":"video_encode","changed_name":"video_encode_1"},{"original_name":"video_transcode","changed_name":"video_transcode_1"}]

I want to pass this as an argument to the shell script. 我想将此作为参数传递给shell脚本。 So, 所以,

$data = json_encode($data);
$cmd = "ssh 10.0.0.37 /usr/tmp/script.sh $data";
exec($cmd, $output);

However, I see that the argument is not correctly read by the shell script. 但是,我看到shell脚本无法正确读取该参数。 I tried putting single quotes around $data, didn't work. 我试图在$ data周围加上单引号,但没有用。 Also, tried using escapeshellarg($data) , still did not work. 另外,尝试使用escapeshellarg($data)仍然无效。

Edit 编辑

The output of echo escapeshellarg($data) is echo escapeshellarg($data)的输出是

'[{"original_name":"pdf_convert","changed_name":"pdf_convert_1"},{"original_name":"video_encode","changed_name":"video_encode_1"},{"original_name":"video_transcode","changed_name":"video_transcode_1"}]'

Also, if there is any other format which can be parsed easily in a shell script, then I would lie to use that format (not necessarily JSON). 另外,如果在shell脚本中可以轻松解析任何其他格式,那么我会撒谎使用该格式(不一定是JSON)。 I see that I may have to use 'jq' to parse json which needs me to install an additional package. 我看到我可能必须使用'jq'来解析json,这需要我安装其他软件包。

Bash isn't very good at accepting a JSON string in as arguments... Bash不太擅长接受JSON字符串作为参数...

One way to get around bash trying to parse the arguments is for your php script to write the JSON string to a file, and for the bash script to parse that file with jq 解决bash尝试解析参数的一种方法是让您的php脚本将JSON字符串写入文件,并让bash脚本使用jq解析该文件。

I was able to use serilaize to send the json data. 我能够使用serilaize发送json数据。 Below is the code. 下面是代码。

$data = json_encode($arg)
$data = escapeshellarg($data);
$data = serialize($data);
$data = str_replace('"','\"',$data);
$cmd = "ssh 10.0.0.37 /usr/tmp/script.sh $data";

I can now get the data in the shell script. 我现在可以在shell脚本中获取数据。

PHP: PHP:

<?php

$json = '[{"original_name":"pdf_convert","changed_name":"pdf_convert_1"},{"original_name":"video_encode","changed_name":"video_encode_1"},{"original_name":"video_transcode","changed_name":"video_transcode_1"}]';

$cmd = $json;
$cmd = addslashes($cmd);
// addslashes does not escapes curly braces
$cmd = strtr($cmd, array('{' => '\\{', '}' => '\\}'));
$cmd = escapeshellarg($cmd);

$cmd = "ssh localhost echo $cmd";

echo "\n$cmd\n\n";

exec($cmd, $output);
var_dump($output)

Shell (/tmp/1.sh): 外壳(/tmp/1.sh):

#!/bin/sh
echo "$1"

Shell Output: 外壳输出:

ssh localhost /tmp/1.sh '[\{\"original_name\":\"pdf_convert\",\"changed_name\":\"pdf_convert_1\"\},\{\"original_name\":\"video_encode\",\"changed_name\":\"video_encode_1\"\},\{\"original_name\":\"video_transcode\",\"changed_name\":\"video_transcode_1\"\}]'

array(1) {
  [0]=>
  string(200) "[{"original_name":"pdf_convert","changed_name":"pdf_convert_1"},{"original_name":"video_encode","changed_name":"video_encode_1"},{"original_name":"video_transcode","changed_name":"video_transcode_1"}]"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM