Subject: Issue with libssh2_channel_wait_closed() after Shell is requested.

Issue with libssh2_channel_wait_closed() after Shell is requested.

From: Ravikanth Sharma <ravi_at_ideadevice.com>
Date: Thu, 25 Oct 2012 16:14:17 +0530

Hi folks,

libssh2_channel_wait_closed() returns -34 (LIBSSH2_ERROR_INVAL) after shell
is requested(libssh2_channel_shell()).
It works fine (returns 0) when there is no shell open on channel.

Is there something extra I need to do to close the channel properly with
shell running?
Why cant I follow libssh2_channel_close() with
libssh2_channel_wait_closed() when the shell is running?

<Code>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

/*******************************************************
 * *
 * NO LICENSE, THIS CODE IS PUBLIC DOMAIN *
 * *
 * Written by Pioz *
 * To compile: "gcc -o ssh_client ssh_client.c -lssh2" *
 *******************************************************/

#include <arpa/inet.h>
#include <stdio.h>
#include <libssh2.h>

int
main (int argc, char **argv)
{
  if (argc != 4)
    {
      fprintf (stderr, "usage: %s host user pass\n", argv[0]);
      return -1;
    }

  char *host = argv[1];
  int port = 22;
  char *user = argv[2];
  char *pass = argv[3];

  int sock;
  LIBSSH2_SESSION *session;
  LIBSSH2_CHANNEL *channel;
  struct sockaddr_in sin;

  // Make a connection on port 22
  sock = socket (AF_INET, SOCK_STREAM, 0);
  sin.sin_family = AF_INET;
  sin.sin_port = htons (port);
  sin.sin_addr.s_addr = inet_addr (host);
  if (connect (sock, (struct sockaddr*)(&sin), sizeof (struct sockaddr_in))
!= 0)
    {
      fprintf (stderr, "Connection failed!\n");
      return -1;
    }
  session = libssh2_session_init ();
  if (libssh2_session_startup (session, sock))
    {
      fprintf (stderr, "SSH connection failed!\n");
      return -1;
    }

  // Authentication
  if (libssh2_userauth_password (session, user, pass))
    {
      fprintf (stderr, "Authentication failed!\n");
      return -1;
    }
  printf ("Authentication successfully!\n");

  // Open terminal
  if (!(channel = libssh2_channel_open_session (session)))
    {
      fprintf (stderr, "Cannot open channel!\n");
      return -1;
    }

  printf ( "\nrequest_pty:\t%d", libssh2_channel_request_pty (channel,
"vt100"));
  printf ( "\nshell:\t%d", libssh2_channel_shell (channel));

  // Close terminal
  printf ( "\nclose:\t%d", libssh2_channel_close (channel));
  printf ( "\nwait_closed:\t%d", libssh2_channel_wait_closed (channel));
  libssh2_channel_free (channel);

  // Disconnect
  libssh2_session_disconnect (session, "Goodbye");
  libssh2_session_free (session);

  printf ("\nAll done.\n");

  return 0;
}

</Code>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Cheers!

Ravi

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2012-10-25