使用PHP和私钥连接到SFTP [英] Connect to SFTP using PHP and private key

查看:580
本文介绍了使用PHP和私钥连接到SFTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一篇又一篇文章,只是找不到适合我所拥有的解决方案".

I have read article after article and just cannot find "the solution" that works for what I have.

我正在尝试使用php脚本通过SFTP上传文件.我已经成功使用Cyber​​Duck进行了连接,但是我需要以编程方式进行此操作.

I am trying to upload files via SFTP using php scripting. I have connected using CyberDuck successfully, but I need to do this programatically.

我有一个在Cyber​​Duck中使用的供应商提供的.PPK文件.我有一个用户名.我有主机名.如果我打开PPK文件,则会看到一些公用线路,专用线路和专用MAC.

I have a .PPK file from the vendor that I used in CyberDuck. I have a username. I have the hostname. If I open the PPK file I see some Public Lines, Private Lines and Private-MAC.

无论如何,我可以使用我拥有的信息来访问服务器来执行我需要做的事情吗?

Is there anyway I can access the server to do what I need to do using the information I have?

这是我正在玩的代码:

<?php if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
?>
<?php
$conn = ssh2_connect('hostname.com', 22);
echo $conn;
ssh2_auth_pubkey_file($conn,'USERNAME','/var/www/html/FILENAME.PPK');

// send a file
ssh2_scp_send($conn, '/var/www/html/FILETOSEND.TXT', 'FILETOSEND.TXT', 0644);
?>

我没有收到任何错误,但文件未显示在服务器上. 我可以确认我的网络主机上已安装SSH2.

I don't receive any errors but the file doesn't show up on the server. I can confirm that SSH2 is installed on my webhost.

感谢您可以提供的任何帮助.

Thanks for any help you can provide.

推荐答案

这个问题已经很老了,但是当我寻找完全相同的答案时,这就是我设法收集到的东西.

The question is quite old, but as I was looking for exactly the same answer, here is what I managed to gather.

.pkk密钥是PuTTY格式的私钥.

.pkk keys are PuTTY format private key.

如果要将其更改为.pem格式,则需要使用以下命令安装腻子工具:

If you want to change that to .pem format, you need to install putty-tools with:

sudo apt install putty-tools

(在Mac上,请使用自制软件安装腻子软件包.对于Windows,我不知道.)

(On a Mac, install putty package with homebrew. For Windows, I have no clue.)

然后您可以更改密钥的格式:

Then you can change the format of the key:

puttygen privatekey.ppk -O private-openssh -o privatekey.pem

以防万一,您想从该私钥中提取公钥(您不需要该答案的其余部分,以防万一),这很容易:

Just in case you want to extract the public key from that private key, (you won't need for the rest of that answer, but just in case) it is quite easy:

openssl rsa -in privatekey.pem -pubout > publickey.pub

第二:使用sFTP登录

现在有了privatekey.pem,您可以使用 phpseclib 通过SFTP连接.首先,使用Composer安装phpseclib:

Second: Login with sFTP

Now that you have the privatekey.pem, you can use phpseclib to connect via SFTP. First, install phpseclib with Composer:

composer require phpseclib/phpseclib

然后在PHP中:

require "vendor/autoload.php";

use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;

$sftp = new SFTP('sftp.server.com');

// create new RSA key
$privateKey = new RSA();

// in case that key has a password
$privateKey->setPassword('private key password');

// load the private key
$privateKey->loadKey(file_get_contents('/path/to/privatekey.pem'));

// login via sftp
if (!$sftp->login('username', $privateKey)) {
    throw new Exception('sFTP login failed');
}

// now you can list what's in here
$filesAndFolders = $sftp->nlist();

// you can change directory
$sftp->chdir('coolstuffdir');

// get a file
$sftp->get('remoteFile', 'localFile');

// create a remote new file with defined content
$sftp->put('newfile.txt', 'new file content');

// put a local file
$sftp->put('remote.txt', 'local.txt', NET_SFTP_LOCAL_FILE);

如果您需要更多信息,请转到 phpseclib sFTP功能列表.

If you want more info, go to the phpseclib sFTP feature list.

这篇关于使用PHP和私钥连接到SFTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆