我有一个问题。我用PHP编写了一个连接远程服务器的脚本。脚本复制文件,看起来像这样
<?php
$pubKey = "/path/to/public/key";
$privKey = "/path/to/private/key";
$srcFile = "/path/to/local/app.apk";
$dstFile = "/path/to/remote/app.apk";
$con = ssh2_connect("xxx.xxx.xx.xxx", "22", array("hostkey" => "ssh-rsa"));
if(ssh2_auth_pubkey_file($con, "user", $pubKey, $privKey, "pw"))
{
sftp = ssh2_sftp($con);
$sftpStream = fopen("ssh2.sftp://" . $sftp . $dstFile, "w");
try
{
if(!$sftpStream) throw new Exception("Could not open remote file: " . $dstFile);
$dataToSend = file_get_contents($srcFile);
if($dataToSend === false) throw new Exception("Could not open local file: " . $srcFile);
if(fwrite( $sftpStream, $dataToSend) === false) throw new Exception("Could not send data from file: " . $srcFile);
fclose($sftpStream);
$shell = ssh2_shell($con, "xterm");
fwrite($shell, "su system");
fwrite($con, "pm install -r " . $dstFile);
echo "passed through";
}
catch (Exception $e)
{
error_log("Exception: " . $e->getMessage());
fclose($sftpStream);
}
}
?>
方法ssh2_scp_send
总是失败,所以我尝试了这种方式,它运行正常。方法ssh2_scp_send
返回错误,如
PHP Warning: ssh2_scp_send(): Failed copying file in /srv/www/htdocs/index.php on line 10
和第10行是方法。我在远程服务器和本地电脑上获得了正确的权限。
我也试过这样的事情
ssh2_exec($con, "su system");
ssh2_exec($con, "pm install -r " . $dstFile);
我也尝试过编写app.apk手册的路径,但没有任何作用。它不会在远程服务器上安装应用程序,但它遍历我的代码,因为我的网站显示消息“已通过”。
command
shell
上remote server
未执行Fruit
-Apple
-Pear
的原因是什么?