【问题标题】:shell linux don't execute commandeshell linux不执行命令
【发布时间】:2019-11-13 23:30:13
【问题描述】:

我有这个命令,它在 shell 上运行良好。 我想用php运行它,但它不起作用 我错过了什么:

已编辑:

$arg="arg1";$arg2= "arg2";

echo shell_exec("sshpass -p ".escapeshellarg($arg)." ssh -t root@00.00.00.00 'asterisk -rx ".escapeshellarg($arg2)."'");

在此更改后输出没有错误,但仍然没有执行命令

已编辑: 原来的命令是这样的:

sshpass -p 'arg1' ssh -t root@00.00.00.00 'asterisk -rx "arg2"'

我想用外部参数解决问题,但它不起作用

【问题讨论】:

  • 你在双引号内使用单引号来使用函数的问题。 echo shell_exec("sshpass -p ".escapeshellarg($arg)." ssh -t root@00.00.00.00 'asterisk -rx ".escapeshellarg($arg2)."'"); 这会起作用。
  • 它不起作用:(

标签: php linux shell exec


【解决方案1】:

如果 php 在安全模式下运行,它将不允许执行shell_exec,如 php 手册中所述:。 https://www.php.net/manual/en/ini.sect.safe-mode.php

您可以禁用某个文件夹的安全模式,如堆栈溢出问题中所述:

How to turn off php safe_mode off for a particular directory in a shared hosting environment?

【讨论】:

  • 您能否按照上述评论中的建议编辑原始帖子以将单引号替换为双引号,并描述输出或您收到的错误。
【解决方案2】:

我终于找到了一个适合仍在搜索的人的好解决方案:

我通过这个链接安装了 php-ssh2(我使用的是 debian 8、php7)

blog.programster.org

在我的情况下,我使用 codeigniter 我按照链接来实现这个类

bitbucket.org

现在我可以毫无问题地在 shell 上执行任何命令

问题就解决了:)

希望对某人有所帮助:)

【讨论】: