PHP运行一个需要SSH密码的脚本
我想运行一个本地脚本,完成后,它会尝试通过SSH向我的服务器发送tarball,因此需要密码。有没有一种方法可以使用ssh2库、proc_open或其他库在PHP中实现这一点 我了解如何在PHP脚本中执行终端命令,当我尝试将此tarball发送到服务器时,我会陷入困境: 到目前为止我拥有的 呼叫代码PHP运行一个需要SSH密码的脚本,php,ssh,Php,Ssh,我想运行一个本地脚本,完成后,它会尝试通过SSH向我的服务器发送tarball,因此需要密码。有没有一种方法可以使用ssh2库、proc_open或其他库在PHP中实现这一点 我了解如何在PHP脚本中执行终端命令,当我尝试将此tarball发送到服务器时,我会陷入困境: 到目前为止我拥有的 呼叫代码 private function run($command, array $arguments = array(), Response $response = null) { $pipes
private function run($command, array $arguments = array(), Response $response = null) {
$pipes = array();
$descriptorspec = array(
array('pipe', 'r'), // STDIN
array('pipe', 'w'), // STDOUT
array('pipe', 'w'), // STDERR
);
$process = proc_open($command, $descriptorspec, $pipes, $this->directory);
foreach ($arguments as $arg) {
// Write each of the supplied arguments to STDIN
fwrite($pipes[0], (preg_match("/\n(:?\s+)?$/", $arg) ? $arg : "{$arg}\n"));
}
if (!$response) {
$response = new Response;
}
$response->addCompletedCommand(stream_get_contents($pipes[1]), $command, $arguments);
$error = stream_get_contents($pipes[2]);
if ($error) {
$response->addError($error, $command, $arguments);
}
// Make sure that each pipe is closed to prevent a lockout
foreach ($pipes as $pipe) {
fclose($pipe);
}
proc_close($process);
return $response;
}
命令
$this->run('shell_script_i_cannot_change_that_runs_ssh', array('password'));
错误:
Host key verification failed
我无法更改脚本,只能从PHP调用它。如果有一个解决方案,它是否只能是PHP您可能只需使用
ssh keygen-R hostname
重置客户端系统上的现有密钥即可
这引入了一个主要的安全漏洞,请小心使用,首先尝试理解ssh问题(例如,在系统上运行命令,然后再次尝试PHP脚本)。好的,我已经解决了这个问题,相当复杂
- 我将
(Apache在OSX上运行的用户/组的名称,更改为系统上的任何名称)添加到组sudoers中,但我只授予运行一个脚本的权限\u www
_www ALL=(ALL) NOPASSWD: /path/to/script/i/couldnt/change
阅读更多关于sudoers文件的信息
- 我在PHP中用
预先编写了调用脚本。这使得软件以我的身份运行二进制文件sudo-u Luke
- 然后,我使用
创建了一个公钥,以允许无密码登录到我的服务器。我建议创建一个只能访问特定文件夹的新用户,然后可能在服务器的ssh\u配置上设置一个ssh-keygen
指令来控制这个新用户Match