Libssh - SSH MESSAGE Unimplemented

2019-06-21 20:33发布

问题:

I'm trying to connect using ssh_connect and libssh, but i get the following error. I have no idea what it means. Any thoughts?

[2014/09/30 00:53:00.015877, 2] channel_open:  Creating a channel 43 with 64000 window and 32768 max packet
[2014/09/30 00:53:00.050776, 1] ssh_packet_unimplemented:  Received SSH_MSG_UNIMPLEMENTED (sequence number 3)
[2014/09/30 00:54:59.949316, 1] ssh_socket_exception_callback:  Socket exception callback: 1 (0)
[2014/09/30 00:54:59.949483, 1] ssh_socket_exception_callback:  Socket error: disconnected
Error allocating SFTP session: Socket error: disconnected

here's the code

** Initialises SSH Session **/ 
ssh_session initialise_ssh(char* host, int port) {

  ssh_session my_ssh_session;
  int verbosity = SSH_LOG_PROTOCOL;

  my_ssh_session = ssh_new();

  ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, host);
  ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);

  rc = ssh_connect(current_session);
  if (rc != SSH_OK)
  {
    fprintf(stderr, "Error connecting host: %s\n",
            ssh_get_error(current_session));

  return my_ssh_session; 
}

回答1:

The ssh_session object you create in your initialise_ssh() function is not completely initialized yet and thus cannot directly be used to create an sftp session like you try to do. It is laking some authentication and thus the socket is closed after a timeout of 120 seconds, yealding your exception.

After your successful ssh_connect() call you should authenticate the host (ssh_is_server_known() function among others) and you must provide a way to authenticate the user: a ssh_userauth_publickey_auto(my_ssh_session, NULL, NULL) would do it if you have public key login set up for the user running your app.

Check "Authenticating the server" and "Authenticating the user" in http://api.libssh.org/master/libssh_tutor_guided_tour.html

After that you are set up to use your session to do some work, do some sftp in your example.



回答2:

You are creating my_ssh_session inside the function. Its scope is limited to the function initialise_ssh. Instead use pointers, or send the session object to be initialized as a parameter.

void initialise_ssh(char* host, int port, session* my_ssh_session_ptr) {

  int verbosity = SSH_LOG_PROTOCOL;

  *my_ssh_session_ptr = ssh_new();

  ssh_options_set(*my_ssh_session_ptr, SSH_OPTIONS_HOST, host);
  ssh_options_set(*my_ssh_session_ptr, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
  ssh_options_set(*my_ssh_session_ptr, SSH_OPTIONS_PORT, &port);

  rc = ssh_connect(current_session);
  if (rc != SSH_OK)
  {
    fprintf(stderr, "Error connecting host: %s\n",
            ssh_get_error(current_session));


}


标签: c networking ssh