这是一个很大的命令。 可能我建议这个,所以它可以更好地理解。 在下面的代码中,我使用了escapeshellarg很多来确保我们所有的shell参数都已正确转义并且不会受到攻击。这也取决于你的变量是否可信,但不应该伤害,除非每个参数变量实际上由多个参数或其他不常见的东西组成。
<?php
$result = shell_exec(
'parallel-ssh -h master.txt "-O StrictHostKeyChecking=no" ' . // SSH command
'-t 5 ' . // 5 second timeout on each host
'-l divx ' . // User
'-i ' . // Inline mode used for troubleshooting.. Take this out once it works.
'-P ' . // Print the output. This will only return it so it is stored in $result
escapeshellarg(
'perl /mnt/nas-storage/EncoderSetup/commTools/con.pl ' . // Executes a perl file
escapeshellarg($input) . ' ' . // $input arg to perl command
escapeshellarg($output) . ' ' . // $output arg to perl command
escapeshellarg($intraperiod) . ' ' . // $intraperiod arg to perl command
escapeshellarg($res) . ' ' . // $res arg to perl command
escapeshellarg($qp) . ' ' . // $qp arg to perl command
escapeshellarg($framerate) . ' ' . // $framerate arg to perl command
escapeshellarg($startframe) . ' ' . // $startframe arg to perl command
escapeshellarg($numgop) . ' ' . // $numgop arg to perl command
escapeshellarg($enc) . ' ' . // $enc arg to perl command
escapeshellarg($cfg) . ' ' . // $cfg arg to perl command
escapeshellarg($sao) . ' ' . // $sao arg to perl command
escapeshellarg($wafrosync) . ' ' . // $wafrosync arg to perl command
escapeshellarg($amp) . ' ' . // $amp arg to perl command
escapeshellarg($tmvp) . ' ' . // $tmvp arg to perl command
escapeshellarg($transkp) . ' ' . // $transkp arg to perl command
escapeshellarg($fasttranskp) . ' ' . // $fasttranskp arg to perl command
escapeshellarg($goploc) // $goploc arg to perl command
)
);
print $result;
这应该适合你,但有一些事情要考虑。 首先,执行它并打印$结果以查看实际输出是什么。如果你得到类似的东西
[FAILURE] server.hostname Exited with error code 255
然后有可能pssh是要求为每个主机的密码。我注意到您正在使用要求输入密码的-A选项。你不能在php中使用shell_exec来做到这一点,因为脚本会挂起,并永远等待输入密码。相反,您需要设置SSH密钥,以便您的第一台云服务器可以在没有密码的情况下ssh进入其他云服务器。设置基于SSH公钥的认证实际上非常简单。但是,如果你以前从来没有做过,我确信有很多关于如何设置的帖子。程序基本上是:
生成公钥和私钥(无密码)。你的第一个云服务器
复制id_rsa.pub文件到的〜/ .ssh不要输入密码短语/在每个次级云服务器的authorized_keys文件
确保你的.ssh文件夹中有每个云服务器的700个权限
确保.ssh/authorized_keys文件在每台云服务器上拥有600个权限。
如果按计划一切顺利,你应该能够从您的主云服务器安全,没有密码在每个云服务器的执行命令。现在,你可以运行你的命令,它应该工作....或者至少给你输出为什么它没有,所以你可以继续排除故障。
另一个问题是shell_exec运行的用户。如果您在主云服务器上运行Web服务器,那么您必须确保当前用户(通常是apache)的.ssh文件夹中的id_rsa文件位于您的apache主目录(通常是/ var/www/)。所以你可以把id_rsa文件放在/var/www/.ssh/文件夹中,并确保它由apache所有。另外,确保它是chmod 600来保护它。
还有其他安全问题,如保护您的id_rsa文件。不要在您的服务器上运行任何不受信任的脚本,也不要使用任何具有将自己的文件上载到自己的网站的虚拟主机。安全问题起作用,因为以apache运行的任何脚本都可以轻松访问,并危及您的id_rsa文件...。任何有权访问此文件的人都可以轻松访问每台云服务器......因此,保护它不应掉以轻心。
是我很困惑,这里的大图是什么?你想达到什么目的? – 2013-07-18 04:18:35
你得到的输出是什么? –
而我没有得到输出。如果我在shell_exec语句之后放置打印语句,它将打印它。所以基本上,我认为它看起来在命令,只是忽略它。 – GiangP