因此,我有5台云机在运行,我的第一台云机被设置为apache服务器。我的目标是让用户点击网页上的提交按钮,它将在我的第一台机器上运行parallel-ssh,并在其他云机器上启动一个脚本。我已经运行了网页,运行了脚本,这是我尝试从index.php启动parallel-ssh
所以"master.txt“在第一个云机器上,它保存了其他云机器的信息。StrictHostKeyChecking用于忽略安全检查。perl命令将在所有云计算机上启动。我知道这个问题很让人困惑,但我对php和perl都是新手,我真的需要这个项目的答案。对不起,这是一个很大的命令,但我必须将它们分成行,因为它不会显示在这里。
发布于 2013-07-19 00:12:21
如果您通过libssh2或phpseclib连接到每台服务器,并像这样在每台机器上运行命令,那么您可能会更轻松?
发布于 2013-07-18 13:19:50
这是一个重要的命令。我是否可以建议这样做,以便更好地理解它。在下面的代码中,我大量使用了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;这对你来说应该是可行的,但是有一些事情需要考虑。首先,执行它并打印出$result,看看实际输出是什么。如果你得到像这样的东西
[FAILURE] server.hostname Exited with error code 255那么,pssh有可能要求为每个主机输入密码。我注意到您使用的是-A选项,该选项要求输入密码。你不能用php中的shell_exec来做这件事,因为这样脚本就会挂起,永远等待密码。相反,您需要设置SSH密钥,以便您的第一个云服务器可以ssh到其他云服务器,而无需密码。设置基于SSH公钥的身份验证实际上非常简单。但如果你以前从未做过,就不会。我相信有很多关于如何设置的帖子。这个过程基本上是:
- Type in this command at your first cloud server: `ssh-keygen`
- Don't enter a passphrase when it asks you
如果一切按计划进行,您应该能够从主云服务器安全地在每个云服务器上执行命令,而不需要密码。现在,您只需运行您的命令,它应该可以工作……或者至少给你输出为什么它没有,这样你就可以继续进行故障排除。
另一个问题是运行shell_exec的用户。如果您在主云服务器上运行web服务器,则必须确保当前用户(通常为apache)在apache主目录(通常为/var/www/)所在的.ssh文件夹中具有id_rsa文件。因此,您应该将id_rsa文件放在/var/www/.ssh/文件夹中,并确保它属于apache。另外,确保它是chmod 600来保护它。
还有其他安全问题,比如保护您的id_rsa文件。不要在你的服务器上运行任何不受信任的脚本,或者使用任何虚拟主机,让用户为自己的网站上传自己的文件。安全问题开始起作用,因为作为apache运行的任何脚本都可以很容易地访问,并危害您的id_rsa文件……啊呀。任何有权访问此文件的人都可以轻松地访问您的每台云服务器...因此,保护它不应该掉以轻心。
https://stackoverflow.com/questions/17714312
复制相似问题