Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP运行一个需要SSH密码的脚本_Php_Ssh - Fatal编程技术网

PHP运行一个需要SSH密码的脚本

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

我想运行一个本地脚本,完成后,它会尝试通过SSH向我的服务器发送tarball,因此需要密码。有没有一种方法可以使用ssh2库、proc_open或其他库在PHP中实现这一点

我了解如何在PHP脚本中执行终端命令,当我尝试将此tarball发送到服务器时,我会陷入困境:

到目前为止我拥有的

呼叫代码

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脚本)。

好的,我已经解决了这个问题,相当复杂

  • 我将
    \u www
    (Apache在OSX上运行的用户/组的名称,更改为系统上的任何名称)添加到组sudoers中,但我只授予运行一个脚本的权限
守则:

 _www    ALL=(ALL) NOPASSWD: /path/to/script/i/couldnt/change
阅读更多关于sudoers文件的信息

  • 我在PHP中用
    sudo-u Luke
    预先编写了调用脚本。这使得软件以我的身份运行二进制文件

  • 然后,我使用
    ssh-keygen
    创建了一个公钥,以允许无密码登录到我的服务器。我建议创建一个只能访问特定文件夹的新用户,然后可能在服务器的ssh\u配置上设置一个
    Match
    指令来控制这个新用户


与php无关的@cmorrissey的可能副本,请注意,我无法更改shell脚本为什么您不能更改shell脚本?你可以通过FTP发送,也可以通过CURL等方式发送。