gpt4 book ai didi

php - 如何使用我的 ec2 key 与 php 进行 scp

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:33 24 4
gpt4 key购买 nike

我在我的 Mac 上使用这样的命令来 scp 到我的远程服务器(在 .sh 文件中——它必须自动完成)。

scp -i my_key.pem index.html ubuntu@<ip>:~/public/index.html;

现在我必须在 Windows 机器上做同样的事情,我想我会用 php 编写它而不是使用 bash,这样代码在 Windows 和 Mac 上是相同的(我们的应用程序也运行一个已经使用的本地服务器php 脚本,这很复杂)。我看过几个例子,但找不到我的确切解决方案。另外,老实说,我对私钥/公钥知之甚少,我想小心点。

我有这样的东西。我不知道要 key 做什么。

$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);

此外,如果我想做一些愚蠢的事情并且有更简单的方法,请告诉我。感谢您的帮助。

~~更新

我的代码现在看起来如下,我收到一个错误 Warning: ssh2_scp_send(): Failure creating remote file: failed to send file

<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

最佳答案

一般格式为

1   <?php
2 $conn = ssh2_connect('example.com', 22);
3 ssh2_auth_pubkey_file(
4 $conn,
5 'username',
6 '/home/username/.ssh/id_rsa.pub',
7 '/home/username/.ssh/id_rsa'
8 );
9 ......

使用 ssh2_scp_send($conn, '/local/filename', '/remote/filename', 0644); 发送文件,其中 0644 是文件权限。

查看 http://www.patcup.com/php-ssh-authentication-using-a-public-key/如果您想继续使用 ssh2 进行此类操作,但建议您使用 https://github.com/phpseclib/phpseclib .

关于php - 如何使用我的 ec2 key 与 php 进行 scp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44179414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com