duan0821 2016-05-04 16:52
浏览 307
已采纳

使用php curl用私钥连接到sftp

I need to write a php script that can connect to a sftp server, get the list of the directories and files in the server, and later download a specific file. I was given the ppk file (I assume this is the private key authentication file) for the authentication part.

I read in a few places that curl can do this.. but I'm not entirely sure how to do it. I tried copy pasting the code from here but my understanding was the code utilizes public keyfile instead of private key.

So here's what I tried to connect to the sftp server

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

The output prints nothing.. so what should I do to actually connect to this sftp properly?

====Update==== Now I'm trying to use phpseclib. Here's my code:

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

But all I got was this message:

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

=============Update: This works!=================

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('.')
  • 写回答

2条回答 默认 最新

  • dongyuan2652 2016-05-04 17:24
    关注

    If you use http://phpseclib.sourceforge.net/ then you don't need to install any additional libraries on the server...

    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('.')
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。