SFTP libssh 失败

SFTP libssh failure

我正在使用 libssh 库并尝试创建一个新的 SFTP 会话。我不断收到

的错误消息

ssh_packet_unimplemented: Received SSH_MSG_UNIMPLEMENTED (sequence number 3)

我检查了远程服务器的系统日志,发现错误

dispatch_protocol_error: type 90 seq 3 [preauth]

出于某种原因,我在尝试使用以下内容时遇到此错误(取自 libssh doc

int sftp_helloworld(ssh_session session) {
  sftp_session sftp;
  int rc;
  sftp = sftp_new(session); // Freezes at this part
  if (sftp == NULL) {
    fprintf(stderr, "Error allocating SFTP session: %s\n", ssh_get_error(session));
    return SSH_ERROR;
  }
  rc = sftp_init(sftp);
  if (rc != SSH_OK) {
    fprintf(stderr, "Error initializing SFTP session: %s.\n", sftp_get_error(sftp));
    sftp_free(sftp);
    return rc;
  }
  sftp_free(sftp);
  return SSH_OK;
}

唯一有效的是实际的 ssh 连接。无论我使用 SCP、SFTP 还是打开远程 shell.

,都会出现同样的错误

我对我的问题进行了很多研究,但找不到任何补救措施。任何帮助都会很棒。

编辑:

在 Amazon EC2 服务器上,我查看了 /var/log/auth.log 并发现:

sshd[24860]: Accepted publickey for ubuntu

sshd[24860]: pam_unix(sshd:session): session opened for user ubuntu by (uid=0)

sshd[24930]: dispatch_protocol_error: type 90 seq 3 [preauth]

在 libssh 代码中,我将 SSH_OPTIONS_LOG_VERBOSITY 设置为 SSH_LOG_PACKET,一切运行顺利,但是当它到达 sftp_new() 时,我收到此错误并冻结在 SSH_MSG_UNIMPLEMENTED 消息,

channel_open: Creating a channel 43 with 64000 window and 32768 max packet

packet_send2: packet: wrote [len=44,padding=19,comp=24,payload=24]

channel_open: Sent a SSH_MSG_CHANNEL_OPEN type session for channel 43

ssh_socket_unbuffered_write: Enabling POLLOUT for socket

ssh_packet_socket_callback: packet: read type 3 [len=12,padding=6,comp=5,payload=5]

ssh_packet_process: Dispatching handler for packet type 3

ssh_packet_unimplemented: Received SSH_MSG_UNIMPLEMENTED (sequence number 3)

编辑2:

我使用以下代码连接到服务器,

ssh_key pKey;
int rc = ssh_pki_import_privkey_file(PC_PRIVATE_KEY_FILE, NULL, NULL, NULL, &pKey);

ssh_session my_ssh_session = ssh_new();

ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, PC_HOST);
ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, PC_USER);
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &PC_PORT);
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &PC_VERBOSITY);

rc = ssh_connect(my_ssh_session);

我被tutorial弄糊涂了。我可能没有写正确连接服务器的代码。

根据 this tutorial,连接到服务器后,您应该对其进行身份验证。即,如果您想使用 public 密钥进行身份验证,请在成功调用 ssh_connect 后添加以下内容:

int rc;
// ...
rc = ssh_userauth_publickey(my_ssh_session, NULL, pKey);
if(rc != SSH_AUTH_SUCCESS) {
    // error
} else {
    // all good, start SFTP/shell/etc
}