Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP执行adb shell并转义字符串_Php_Adb - Fatal编程技术网

从PHP执行adb shell并转义字符串

从PHP执行adb shell并转义字符串,php,adb,Php,Adb,我正在尝试将一个工作的bash脚本转换为PHP代码 #!/bin/bash phonenumber="$1" message="$2" # check the args... # double quotes inside $message must be escaped. Also prevents code injection. message=${message//\"/\\\"} adb shell "am startservice --user 0 -n com.android.sh

我正在尝试将一个工作的bash脚本转换为PHP代码

#!/bin/bash
phonenumber="$1"
message="$2"

# check the args...
# double quotes inside $message must be escaped. Also prevents code injection.
message=${message//\"/\\\"}

adb shell "am startservice --user 0 -n com.android.shellms/.sendSMS -e \"contact\" $phonenumber -e msg \"$message\""
上面的代码有效。 在PHP中,当消息包含新行时,下一个代码不起作用:

function sendsms ($m, $to, $n) {
    echo "About to sent an sms to $to [$n]\n";

    // escape some chars in $m
    $m = addslashes($m);

    adb_shell_exec ("am startservice --user 0 -n com.android.shellms/.sendSMS -e contact \"$to\" -e msg \"$m\"");

}

function adb_shell_exec ($s) {
    shell_exec("adb shell " . addslashes ($s));
}
我收到了错误消息:

$ php -r "require 'sim-android-v8.php'; sendsms('Message with one double \" quote final solution and new line','+32*******31','Pierre François');" About to sent an sms to +32*******31 [Pierre François] /system/bin/sh: no closing quote sh: 2: and: not found $php-r“需要'sim-android-v8.php';sendsms('消息带双引号\'最终解决方案 和新线","32*******31","皮埃尔·弗朗索瓦";;" 即将向+32*******31[皮埃尔·弗朗索瓦]发送短信 /系统/bin/sh:无结束报价 sh:2:和:未找到 我不明白为什么它可以在bash中工作,但不能在PHP中工作。

相同的bash脚本

function sendsms ($m, $to, $n) {
echo "About to sent an sms to $to [$n]\n";

$m = escapeshellarg(addslashes($m)); // use escape for shell
$to = escapeshellarg($to);
// double quotes around command for sub shell
shell_exec('adb shell "am startservice --user 0 -n com.android.shellms/.sendSMS -e contact '.$to.' -e msg '.$m.'"');
}
相同的bash脚本

function sendsms ($m, $to, $n) {
echo "About to sent an sms to $to [$n]\n";

$m = escapeshellarg(addslashes($m)); // use escape for shell
$to = escapeshellarg($to);
// double quotes around command for sub shell
shell_exec('adb shell "am startservice --user 0 -n com.android.shellms/.sendSMS -e contact '.$to.' -e msg '.$m.'"');
}

多亏了FAEWZX,我发现了函数escapeshellarg()的正确用法。他上面给出的答案在大多数情况下都有效,但不是防水的。在我看来,下面的代码更好,因为到目前为止它覆盖了100%的情况,使用了escapeshellarg()两次并递归使用

function sendsms ($m, $to, $n) {
    echo "About to sent an sms to $to [$n]\n";

    $subshell = 'am startservice --user 0' .
      ' -n com.android.shellms/.sendSMS' .
      ' -e contact ' . escapeshellarg($to) . 
      ' -e msg ' . escapeshellarg($m);

    $shell = 'adb shell ' . escapeshellarg($subshell);

    shell_exec($shell);
}

多亏了FAEWZX,我发现了函数escapeshellarg()的正确用法。他上面给出的答案在大多数情况下都有效,但不是防水的。在我看来,下面的代码更好,因为到目前为止它覆盖了100%的情况,使用了escapeshellarg()两次并递归使用

function sendsms ($m, $to, $n) {
    echo "About to sent an sms to $to [$n]\n";

    $subshell = 'am startservice --user 0' .
      ' -n com.android.shellms/.sendSMS' .
      ' -e contact ' . escapeshellarg($to) . 
      ' -e msg ' . escapeshellarg($m);

    $shell = 'adb shell ' . escapeshellarg($subshell);

    shell_exec($shell);
}

很棒的FAEWZX,你让我开心了。它很有效。我只有一个问题:如果消息包含一个单引号('),那么输出在它前面包含一个反斜杠(\)。这是因为函数addslashes不仅在双引号(')之前添加了反斜杠,而且在单引号(')之前也添加了反斜杠。但是,这并不重要,代码正在运行。请参阅下面的另一个解决方案。很棒的FAEWZX,你让我开心。它起作用了。我只有一个问题:如果消息包含一个单引号('),则输出在其前面包含一个反斜杠(\)。这是由于函数addslashes不仅在双引号(“)之前添加了反斜杠,而且在单引号(')之前也添加了反斜杠。但是,这并不重要,代码正在运行。请参阅下面的另一个解决方案。