【发布时间】:2017-10-26 00:56:59
【问题描述】:
我在我的 Mac 上使用这样的命令来 scp 到我的远程服务器(在 .sh 文件中 - 它必须自动完成)。
scp -i my_key.pem index.html ubuntu@<ip>:~/public/index.html;
现在我必须在 Windows 机器上做同样的事情,我想我会用 php 编写它而不是使用 bash,这样代码在 Windows 和 Mac 上是相同的(我们的应用程序也运行一个已经使用的本地服务器php 脚本,这很复杂)。我查看了一些示例,但找不到我的确切解决方案。另外,老实说,我对私钥/公钥知之甚少,我想小心。
我有这样的东西。我不知道该怎么办。
$hostname = '21.232.foo.bar';
$sourceFile = 'index.html';
$targetFile = '~/public/indexx.html';
// SSH Key File — I'm guessing this should be private, not public
private $ssh_auth_priv = '~/.ec2/my_key.pem';
$connection = ssh2_connect($hostname, 22);
ssh2_scp_send($connection, $sourceFile, $targetFile, 0777);
另外,如果我想做一些愚蠢的事情并且有更简单的方法,请告诉我。感谢您的帮助。
~~ 更新
我的代码现在如下所示,我收到错误警告:ssh2_scp_send():创建远程文件失败:发送文件失败
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$hostname = '13.124.foo.bar';
$sourceFile = 'foo.txt';
$targetFile = '~/public2/foo.txt';
$connection = ssh2_connect($hostname, 22);
ssh2_auth_pubkey_file(
$connection,
'ubuntu',
'~/.ec2/id_rsa.pub',
'~/.ec2/bh.pem'
);
ssh2_scp_send($connection, $sourceFile, $targetFile, 0644);
?>
</body>
</html>
这是托管在我的 localhost:8000 上
【问题讨论】:
标签: php amazon-ec2