我需要PHP使用密钥和帐户上的PPK密钥和密码通过SFTP下载文件

时间:2016-09-03 22:17:04

标签: php sftp phpseclib

首先我按照以下说明安装了phpseclib: http://phpseclib.sourceforge.net/pear.htm

但是我收到了这条错误消息,上面写着:第512行/usr/local/lib/php/Net/SFTP.php中的预期SSH_FXP_VERSION 我尝试通过telnet连接,我可以毫无问题地连接到端口22,所以我知道它不是防火墙问题。 请注意,如果有更简单的方法,我不需要使用phpseclib,例如直接shell命令,因为我已经知道我正在下载的文件的名称。我只是不知道该怎么做。

所以这是我目前的代码:

<?php
define('NET_SSH2_LOGGING', 2);
include'Net/SSH2.php';
include'Crypt/RSA.php';
include'Net/SFTP.php';
header('Content-Type: text/html; charset=Windows-1252');

$rsa = new Crypt_RSA();

$rsa->setPassword('mypassword');

$rsa->loadKey('PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Public-Lines: 6
[base64 data]
Private-Lines: 14
[base64 data]
Private-MAC: [hex data]');

$sftp = new Net_SFTP('myhostname', 22);

if (!$sftp->login('myusername', $rsa)) {
    exit('login failed');
    exit;
}

print_r($sftp->nlist()); // == $sftp->nlist('.')
print_r($sftp->rawlist()); // == $sftp->rawlist('.')

打印出: 注意:第512行/usr/local/lib/php/Net/SFTP.php中的预期SSH_FXP_VERSION 登录失败

编辑:我被要求显示日志输出。这是其中的一部分。它非常漫长而且充满了神秘的十六进制,我删除了大部分内容以节省空间。

<-
00000000  53:53:48:2d:32:2e:30:2d:4f:70:65:6e:53:53:48:5f  SSH-2.0-OpenSSH_
00000010  37:2e:32:70:32:20:55:62:75:6e:74:75:2d:34:75:62  7.2p2 Ubuntu-4ub
00000020  75:6e:74:75:32:2e:31:0d:0a                       untu2.1..

->
00000000  53:53:48:2d:32:2e:30:2d:70:68:70:73:65:63:6c:69  SSH-2.0-phpsecli
00000010  62:5f:31:2e:30:20:28:6f:70:65:6e:73:73:6c:2c:20  b_1.0 (openssl, 
00000020  62:63:6d:61:74:68:29:0d:0a                       bcmath)..

<- NET_SSH2_MSG_KEXINIT (since last: 0.2463, network: 0.0733s)
-> NET_SSH2_MSG_KEXINIT (since last: 0.0005, network: 0.0001s)
-> NET_SSH2_MSG_KEXDH_INIT (since last: 0.0247, network: 0s)
<- NET_SSH2_MSG_KEXDH_REPLY (since last: 0.1589, network: 0.1589s)
-> NET_SSH2_MSG_NEWKEYS (since last: 0.0311, network: 0.0001s)
<- NET_SSH2_MSG_NEWKEYS (since last: 0.0001, network: 0s)
-> NET_SSH2_MSG_SERVICE_REQUEST (since last: 0.0011, network: 0s)
00000000  00:00:00:0c:73:73:68:2d:75:73:65:72:61:75:74:68  ....ssh-userauth
<- NET_SSH2_MSG_SERVICE_ACCEPT (since last: 0.1807, network: 0.1806s)
00000000  00:00:00:0c:73:73:68:2d:75:73:65:72:61:75:74:68  ....ssh-userauth
-> NET_SSH2_MSG_USERAUTH_REQUEST (since last: 0.0139, network: 0.0001s)
<- NET_SSH2_MSG_USERAUTH_PK_OK (60) (since last: 0.0735, network: 0.0734s)
-> NET_SSH2_MSG_USERAUTH_REQUEST (since last: 0.0821, network: 0.0001s)
<- NET_SSH2_MSG_USERAUTH_SUCCESS (since last: 0.0738, network: 0.0737s)
-> NET_SSH2_MSG_CHANNEL_OPEN (since last: 0.0002, network: 0.0001s)
00000000  00:00:00:07:73:65:73:73:69:6f:6e:00:00:01:00:7f  ....session.....
00000010  ff:ff:ff:00:00:40:00                             .....@.
<- NET_SSH2_MSG_GLOBAL_REQUEST (since last: 0.0722, network: 0.0001s)
-> NET_SSH2_MSG_REQUEST_FAILURE (since last: 0.0002, network: 0s)
<- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (since last: 0.1085, network: 0.1084s)
00000000  00:00:01:00:00:00:00:00:00:00:00:00:00:00:80:00  ................
-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0.0003, network: 0.0001s)
00000000  00:00:00:00:00:00:00:09:73:75:62:73:79:73:74:65  ........subsyste
00000010  6d:01:00:00:00:04:73:66:74:70                    m.....sftp
<- NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST (since last: 0.0732, network: 0.0001s)
00000000  00:00:01:00:00:20:00:00                          ..... ..
<- NET_SSH2_MSG_CHANNEL_SUCCESS (since last: 0.0001, network: 0s)
00000000  00:00:01:00                                      ....
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0001s)
00000000  00:00:00:00:00:00:00:09:00:00:00:05:01:00:00:00  ................
00000010  03                                               .
<- NET_SSH2_MSG_CHANNEL_EOF (since last: 0.0012, network: 0.0011s)
00000000  00:00:01:00                                      ....
<- NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0.0001, network: 0s)
00000000  00:00:01:00:00:00:00:0b:65:78:69:74:2d:73:74:61  ........exit-sta
00000010  74:75:73:00:00:00:00:01                          tus.....
<- NET_SSH2_MSG_CHANNEL_CLOSE (since last: 0.0001, network: 0s)
00000000  00:00:01:00                                      ....
-> NET_SSH2_MSG_CHANNEL_CLOSE (since last: 0.0001, network: 0s)
00000000  00:00:00:00                                      ....

编辑:这就是Filezilla所做的:

Status: Connecting to [host]...
Response:   fzSftp started, protocol_version=6
Command:    keyfile "C:\private_key.ppk"
Command:    open "[user@host]" 22
Command:    Pass: *********
Status: Connected to [host]
Error:  Received unexpected end-of-file from SFTP server
Error:  Could not connect to server

0 个答案:

没有答案