简体   繁体   English

从PHP的exec函数运行`scp`脚本

[英]Running `scp` script from exec function in PHP

I am trying to SCP a file from one server to an external mail server. 我正在尝试将SCP文件从一台服务器发送到外部邮件服务器。

In my code I use: 在我的代码中,我使用:

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

In my Script.sh I have: 在我的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/.

When I call the script with a file via the CLI, the script runs fine and the file transfers. 当我通过CLI调用带有文件的脚本时,脚本运行良好并且文件传输。 When I run it in PHP as the apache/www-data user the exec() call fails with a code 126, which translates to: "Command invoked cannot execute". 当我以apache / www-data用户身份在PHP中运行它时, exec()调用失败,并返回代码126,该代码转换为:“调用的命令无法执行”。

Extra notes: 额外说明:

I noticed for the first time every user tries this, they get a confirmation of the RSA fingerprint. 我第一次注意到每个用户都尝试这样做,他们得到了RSA指纹的确认。 I thought that the script was stalling on that and that would be the problem. 我以为脚本就此停滞了,那就是问题所在。 I manually went in as the www-data user and confirmed the fingerprint. 我以www-data用户身份手动进入并确认了指纹。 The problem still exists. 问题仍然存在。

After many tries, I created a replica set up of the folders, scripts and files and opened the permissions up completely - all the way down to root to see if I had the wrong permissions set. 经过多次尝试,我创建了一个由文件夹,脚本和文件组成的副本集,并完全打开了权限-一直向下到root,以查看是否设置了错误的权限。 I still receive a 126 error. 我仍然收到126错误。

My project manager was against using the idea of using the PHP SSH2/SCP functions for the reason that if the fundamental bash script is failing, then it will fail for the same reasons with the SSH2/SCP functions as well. 我的项目经理反对使用PHP SSH2 / SCP函数的想法,因为如果基本的bash脚本失败,则由于相同的原因,SSH2 / SCP函数也会失败。

I have a hunch that the above is the wrong idea because the script works via CLI and not via Apache, while the PHP SCP functions are meant to be run from Apache. 我有一种预感,上面的想法是错误的,因为脚本是通过CLI而不是通过Apache来运行的,而PHP SCP函数是从Apache运行的。

Any insight on where my problem (calling SCP inside a script from an exec() call) stems from or hard evidence that the PHP SCP functions will work would be appreciated. 任何关于我的问题(从exec()调用中的脚本中调用SCP)的问题的见解,都源于或有确凿的证据表明PHP SCP函数将起作用。

I recently published a project that allows PHP to obtain and interact with a real Bash shell. 我最近发布了一个项目,该项目允许PHP获取真正的Bash shell并与之交互。 Get it here: https://github.com/merlinthemagic/MTS 在这里获取它: https : //github.com/merlinthemagic/MTS

After downloading you would simply use the following code: 下载后,您只需使用以下代码:

//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');

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

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