从PHP

时间:2016-02-05 17:53:53

标签: php bash apache

我正在尝试从一台服务器到外部邮件服务器的SCP文件。

在我的代码中我使用:

exec('/full/path/to/script.sh' '/full/path/to/file_to_transfer.txt');

在我的Script.sh中,我有:

scp  -i /full/path/to/private.key $1 [user]@[ip]:/opt/mailman/mailman-bundler/venv-3.4/lib/python3.4/site-packages/mailman/templates/en/.

当我通过CLI使用文件调用脚本时,脚本运行正常并且文件传输。当我在PHP中作为apache / www-data用户运行它时,exec()调用失败,代码为126,转换为:“调用的命令无法执行”。

额外说明:

我注意到每个用户第一次尝试这个,他们得到了RSA指纹的确认。我认为脚本停滞不前,那就是问题所在。我手动进入www-data用户并确认指纹。问题依然存在。

经过多次尝试,我创建了文件夹,脚本和文件的副本设置,并完全打开了权限 - 一直到root用户,看看我是否设置了错误的权限。我仍然收到126错误。

我的项目经理反对使用PHP SSH2/SCP functions的想法,因为如果基本的bash脚本失败,那么它也会因为SSH2 / SCP功能的原因而失败。

我有一种预感,上述是错误的想法,因为脚本通过CLI工作,而不是通过Apache工作,而PHP SCP功能则是从Apache运行。

任何关于我的问题(从exec()调用中调用脚本中的SCP)的任何见解都源于或者有证据证明PHP SCP功能将起作用。

1 个答案:

答案 0 :(得分:0)

我最近发布了一个项目,允许PHP获取真正的Bash shell并与之交互。在此处获取:https://github.com/merlinthemagic/MTS

下载后,您只需使用以下代码:

//if the script does not require root permissions you can change the
//second argument on getShell() to false. That will return a bash shell
//with permissions from www-data or apache user.

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/full/path/to/script.sh' '/full/path/to/file_to_transfer.txt');
相关问题