使用php curl用私鑰連接到sftp

使用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

  1.  
    $ch = curl_init();
  2.  
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
  3.  
    curl_setopt($ch, CURLOPT_URL, 'sftp://233.42.20.115/');
  4.  
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
  5.  
    curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE,'megpxl_private.ppk');
  6.  
    curl_setopt($ch, CURLOPT_SSH_AUTH_TYPES,CURLSSH_AUTH_AGENT);
  7.  
    $output = curl_exec ($ch);
  8.  
    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:

  1.  
    require_once 'phpseclib/Net/SSH2.php';
  2.  
    require_once 'phpseclib/Net/SFTP.php';
  3.  
    require_once 'phpseclib/Crypt/RSA.php';
  4.  
    require_once 'phpseclib/Crypt/RC2.php';
  5.  
    require_once 'phpseclib/Crypt/RC4.php';
  6.  
    require_once 'phpseclib/Math/BigInteger.php';
  7.  
     
  8.  
    set_include_path('phpseclib/Net/');
  9.  
     
  10.  
    $privatekey = file_get_contents('sftp_private.txt');
  11.  
     
  12.  
    $rsa = new Crypt_RSA();
  13.  
    $rsa->loadKey(file_get_contents("private.ppk"));
  14.  
    $sftp = new Net_SFTP('233.12.20.225', 22);
  15.  
    if (!$sftp->login("myUserName", $rsa)) {
  16.  
    exit('Login Failed');
  17.  
    }
  18.  
    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!=================

  1.  
    require_once 'phpseclib/Net/SSH2.php';
  2.  
    require_once 'phpseclib/Net/SFTP.php';
  3.  
    require_once 'phpseclib/Crypt/RSA.php';
  4.  
    require_once 'phpseclib/Math/BigInteger.php';
  5.  
     
  6.  
    set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
  7.  
     
  8.  
    $privatekey = file_get_contents('sftp_private.txt');
  9.  
     
  10.  
    $rsa = new Crypt_RSA();
  11.  
    $rsa->loadKey(file_get_contents(mykey.ppk"));
  12.  
    $sftp = new Net_SFTP('223.22.20.122', 22);
  13.  
    if (!$sftp->login("usrPMEGPXLtxn", $rsa)) {
  14.  
    exit('Login Failed');
  15.  
    }
  16.  
     
  17.  
    print_r($sftp->nlist()); // == $sftp->nlist('.')
  18.  
    print_r($sftp->rawlist()); // == $sftp->rawlist('.')
  19.  

 

 

 

 

 

 

 

<?php

//require_once 'abstract.php';
require_once "shell/vendor/autoload.php";
use phpseclib\Net\SFTP;

/**
 * Class HP_Shell_Stock
 */
class HP_Shell_File
{
    const REMOTE_DIR = '/home/server/Public/';
    private $host = '192.168.8.113';
    private $server = 'server';
    private $password = ' ';

    public function getLocalDir()
    {
        return Mage::getBaseDir('var') . '/content_xml_downloading';
    }

    public function getLocalDestDir()
    {
        return Mage::getBaseDir('media') . '/contents_xml';
    }

    public function run(){
        $nameArr = array();
        $needFil = array();
        $sftp = new SFTP($this->host);
        if (!$sftp->login($this->server,$this->password)) {
            throw new Exception("Login failed");
        }else{
            echo "<h1>it works</h1><br>";
            //download
            $fileName = $sftp->nlist(self::REMOTE_DIR);
            foreach ($fileName as $name) {
                if (strstr($name , 'cap_cn_zh_xml_')) {
                    echo $name.'<br>';
                    array_push($needFil,$name);
                    $sftp->get(self::REMOTE_DIR.$name,$this->getLocalDir()."/$name");
                    preg_match_all("/cap_cn_zh_xml_[\d]{4}_([\d]{6})/",$name,$out,PREG_SET_ORDER);
                    array_push($nameArr,$out[0][1]);
                }
            }
            $this->extractFile($needFil);
            $this->createFile($nameArr,$fileName);
            $this->moveFiles
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章