使用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('.')
-
<?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($nameArr,$needFil,$fileName);
}
}
/**
* 移动文件到指定文件
*/
public function moveFiles($nameArr,$needFil,$fileName){
$sftp = new SFTP($this->host);
if (!$sftp->login($this->server,$this->password)) {
throw new Exception("Login failed");
}else{
$remoteFil = array();
foreach ($fileName as $value){
preg_match_all("/^\d{6}/",$value,$out,PREG_SET_ORDER);
if (!empty($out)){
array_push($remoteFil,$out[0][0]);
}
}
$sftp->chdir(self::REMOTE_DIR);//改变目录
foreach ($needFil as $valueNd){
foreach (array_unique($nameArr) as $key=>$valueNa){
if (in_array($valueNa,$remoteFil)){
if (strstr($valueNd,$valueNa)){
$sftp->rename($valueNd,$valueNa.'/'.$valueNd);
}
}else{
$sftp->mkdir($valueNa);
$sftp->rename($valueNd,$valueNa.'/'.$valueNd);
}
}
}
}
}
/**
* 解压文件
*/
public function extractFile($name) {
echo $_SERVER['PATH_INFO'];
if (count($name) != 0){
foreach ($name as $value){
$zip=new ZipArchive();
chmod("/usr/share/nginx/html/getremotefiles/$value",0777);
if($zip->open("/usr/share/nginx/html/getremotefiles/$value")===TRUE){
$zip->extractTo("/usr/share/nginx/html/getremotefiles/abc");
file_put_contents("/usr/share/nginx/html/hp.php9.cc/var/log/success.txt",$value."\n",FILE_APPEND);
$zip->close();
}else{
file_put_contents("/usr/share/nginx/html/hp.php9.cc/var/log/failed.txt",$value."\n",FILE_APPEND);
}
}
}
}
/**
* 根据文件创建文件夹
*/
// public function createFile($arr,$fiName) {
// $sftp = new SFTP("192.168.8.113");
// if (!$sftp->login("server"," ")){
// throw new Exception("Login failed");
// }else{
// foreach (array_unique($arr) as $key=>$one) {
// foreach ($fiName as $two){
// if ($one == $two){
// unset($arr[$key]);
// }
// }
// }
// $sftp->chdir("/home/server/Public/");//改变目录
// foreach (array_unique($arr) as $value) { //创建目录
// $sftp->mkdir($value);
// }
// }
// return;
// }
}
$shell = new HP_Shell_File();
$shell->run();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报