【问题标题】:use php curl to connect to sftp with private key使用 php curl 使用私钥连接到 sftp
【发布时间】:2016-05-04 16:52:02
【问题描述】:

我需要编写一个可以连接到sftp服务器的php脚本,获取服务器中的目录和文件列表,然后下载特定的文件。我得到了认证部分的 ppk 文件(我假设这是私钥认证文件)。

我在几个地方读到 curl 可以做到这一点。但我不完全确定如何做到这一点。我尝试从here 复制粘贴代码,但我的理解是代码使用公钥文件而不是私钥。

这就是我尝试连接到 sftp 服务器的内容

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE); 
curl_setopt($ch, CURLOPT_URL, 'sftp://233.42.20.115/');
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE,'megpxl_private.ppk');
curl_setopt($ch, CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_AGENT);
$output = curl_exec ($ch);
print_r($output);

输出什么也没有打印出来。那么我应该怎么做才能正确连接到这个 sftp?

====更新==== 现在我正在尝试使用 phpseclib。这是我的代码:

require_once 'phpseclib/Net/SSH2.php';
require_once 'phpseclib/Net/SFTP.php';
require_once 'phpseclib/Crypt/RSA.php';
require_once 'phpseclib/Crypt/RC2.php';
require_once 'phpseclib/Crypt/RC4.php';
require_once 'phpseclib/Math/BigInteger.php';

set_include_path('phpseclib/Net/');

$privatekey = file_get_contents('sftp_private.txt');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents("private.ppk"));
$sftp = new Net_SFTP('233.12.20.225', 22);
if (!$sftp->login("myUserName", $rsa)) {
    exit('Login Failed');
}
print_r($sftp);

但我得到的只是这条消息:

No compatible server to client encryption algorithms found in /var/www/html/phpseclib/Net/SSH2.php on line 1375

=============更新:这行得通!=================

require_once 'phpseclib/Net/SSH2.php';
require_once 'phpseclib/Net/SFTP.php';
require_once 'phpseclib/Crypt/RSA.php';
require_once 'phpseclib/Math/BigInteger.php';

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

$privatekey = file_get_contents('sftp_private.txt');

$rsa = new Crypt_RSA();
$rsa->loadKey(file_get_contents(mykey.ppk"));
$sftp = new Net_SFTP('223.22.20.122', 22);
if (!$sftp->login("usrPMEGPXLtxn", $rsa)) {
    exit('Login Failed');
}

print_r($sftp->nlist()); // == $sftp->nlist('.')
print_r($sftp->rawlist()); // == $sftp->rawlist('.')

【问题讨论】:

标签: php curl sftp


【解决方案1】:

如果你使用http://phpseclib.sourceforge.net/,那么你不需要在服务器上安装任何额外的库...

include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

print_r($sftp->nlist()); // == $sftp->nlist('.')
print_r($sftp->rawlist()); // == $sftp->rawlist('.')

(来自http://phpseclib.sourceforge.net/new/sftp/examples.html)

【讨论】:

  • 我尝试使用 phpseclib,但我得到的只是“在第 1375 行的 phpseclib/Net/SSH2.php 中找不到与客户端加密算法兼容的服务器”。顺便说一句,我已经用我用于此的 phpseclib 代码进行了更新。
  • 太可惜了,我已经用过它了,它很容易工作
  • 这行得通,我只需要添加 set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
【解决方案2】:

PHP 已经有一个本机 ssh 扩展...查看您的主机是否在常规 phpinfo() 中激活了此扩展;

您正在寻找: http://php.net/manual/en/wrappers.ssh2.php

更具体的: http://php.net/manual/en/function.ssh2-exec.php

从那里你可以做这样的事情:

// Generate connection
$conn = ssh2_connect('your.site.com', 22);
ssh2_auth_password($conn, 'username', 'password');

// Set the connection setup for files
$sftp = ssh2_sftp($connection);

// List the Directory (if this is a UNIX system)
$ls = ssh2_exec($conn, 'path/to/dir ls');
echo $ls; // or create an array with a foreach function.

// To read the file
$file = fopen("ssh2.sftp://$sftp/path/to/file", 'r');
// you can grab the file using the $file bytes.

// Or just download the file.
ssh2_scp_recv($conn, '/path/remote/filename', '/path/local/filename');

希望这会有所帮助。

【讨论】:

  • 很遗憾,目前我的主机没有安装 ssh2...稍后需要查看。
  • 问题专门要求密钥认证,回答使用用户名/密码。
猜你喜欢
  • 2015-03-31
  • 2020-10-28
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
相关资源
最近更新 更多