使用 php curl 使用私钥连接到 sftp
use php curl to connect to sftp with private key
我需要编写一个php脚本,可以连接到sftp服务器,获取服务器中目录和文件的列表,然后下载特定文件。我得到了身份验证部分的 ppk 文件(我假设这是私钥身份验证文件)。
我在一些地方读到 curl 可以做到这一点..但我不完全确定如何去做。我尝试从 here 复制粘贴代码,但我的理解是代码使用 public 密钥文件而不是私钥。
这就是我尝试连接到 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 已经有一个本地 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');
希望对您有所帮助。
如果您使用 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)
我需要编写一个php脚本,可以连接到sftp服务器,获取服务器中目录和文件的列表,然后下载特定文件。我得到了身份验证部分的 ppk 文件(我假设这是私钥身份验证文件)。
我在一些地方读到 curl 可以做到这一点..但我不完全确定如何去做。我尝试从 here 复制粘贴代码,但我的理解是代码使用 public 密钥文件而不是私钥。
这就是我尝试连接到 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 已经有一个本地 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');
希望对您有所帮助。
如果您使用 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)