2016-02-19 66 views
-1

我想运行一个本地脚本,当完成尝试通过SSH发送tar包到我的服务器并因此需要密码时。有没有办法使用ssh2库,proc_open或其他库来实现这一点在PHP中。PHP运行期望SSH密码的脚本

我知道如何从PHP脚本中执行终端命令,我被卡住当我尝试这个压缩包发送到我的服务器:

我有什么到目前为止

主叫代码

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

+0

的可能的复制[如何使用bash进入SSH密码?] (http://stackoverflow.com/questions/16928004/how-to-enter-ssh-password-using-bash) – cmorrissey

+0

@cmorrissey这与php没有任何关系,并且请注意,我无法更改shell脚本 –

+0

为什么你不能更改shell脚本?你可以FTP它http://php.net/manual/en/book.ftp.php,你可以通过CURL等发送它等。 – cmorrissey

回答

0

您可以使用ssh-keygen -R hostname重置客户端系统上的现有密钥。

这引入了一个主要的安全漏洞,请仔细使用并尝试首先了解ssh问题(例如在系统上运行命令并再次尝试您的PHP脚本)。