2016-05-04 72 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

問題具體要求進行密鑰驗證,並使用用戶名/密碼進行回答。 –