2016-05-04 75 views
1

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

我在一些卷曲可以做到这一点的地方阅读..但我不完全确定如何做到这一点。我试着复制粘贴从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('.') 
+0

你可以随时使用'curl_error($ CH)'得到最后的错误:http://php.net/manual/en/function.curl-error.php –

回答

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

+0

我试过使用phpseclib,但我得到的只是“在1375行上没有兼容的服务器到在phpseclib/Net/SSH2.php中找到的客户端加密算法”。顺便说一句,我更新了我用于此的phpseclib代码。 – imin

+0

这是一个耻辱,我用它,它很容易工作 – Barry

+1

这个工程,我只需要添加set_include_path(get_include_path()。PATH_SEPARATOR。'phpseclib'); – imin

-1

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

希望这有助于。

+0

不幸的是,目前我的主机没有安装ssh2 ...需要稍后再看看。 – imin

+0

问题具体要求进行密钥验证,并使用用户名/密码进行回答。 –