Hello!

I try to write a small simple ssh client program to learn how to work with
libssh2.
I have the next code:

...
/* Request a shell */
channel = libssh2_channel_open_session(session);
if (!channel) {
fprintf(stderr, "Unable to open a session\n");
return (EXIT_FAILURE);
}

/* Request a terminal with 'vanilla' terminal emulation */
rc = libssh2_channel_request_pty(channel, "vanilla");
if (rc) {
fprintf(stderr, "Failed requesting pty\n");
return (EXIT_FAILURE);
}

/* Open a SHELL on that pty */
rc = libssh2_channel_shell(channel);
if (rc) {
fprintf(stderr, "Unable to request shell on allocated pty\n");
return (EXIT_FAILURE);
}

/* Main loop starts here.
 * In it you will be requested to input a command
 * command will be executed at remote side
 * an you will get output from it */
do {
/* Request for command input */
printf("$ ");
fgets(command, BUFSIZ, stdin);
printf("Command is %s", command);
if (strcmp(command, "\n") == 0) {
printf("Empty command\n");
continue;
}

/* Write command to stdin of remote shell */
rc = libssh2_channel_write(channel, command, strlen(command));
printf("Channel write return value is %d\n", rc);

/* Read output from remote side */
rc = libssh2_channel_read(channel, inputbuf, BUFSIZ);
printf("Channel write return value is %d\n", rc);
printf("Remote side output:\n %s\n", inputbuf);

} while (strcmp(command, EXIT_COMMAND) != 0);
/* Main loop ends here */
...

The problem is that I get the output of the current command only when
executing the next one. How should I know that execution of the current
command is finished on remote side and I can use lbssh2_channel_read to
read it?

The whole program it attachment.

Thank you

BR
Ivan Tretyakov
/*
 ============================================================================
 Name		 : SmallSimpleSSH.c
 Author		 : Ivan Tretyakov
 Version	 : 0.1
 Copyright   : GPLv3
 Description : Libssh2 usage example
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libssh2.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define EXIT_COMMAND	"exit\n"

/* Main function */
int main(void) {

	/* Configurable variables */
	const char *username = "root";
	const char *password = "root";
	const char *hostaddr = "192.168.0.13";

	/* Variables to don't touch */
	int rc;
	int sock;
	struct sockaddr_in sin;
	LIBSSH2_SESSION *session;
	LIBSSH2_CHANNEL *channel;
	char command[BUFSIZ];
	char inputbuf[BUFSIZ];

	/* Libss2 init block */
	rc = libssh2_init(0);
	if (rc) {
		fprintf(stderr, "Error: libssh_init()\n");
		return (EXIT_FAILURE);
	}

	/* Creating socket */
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1) {
		perror("socket");
		return (EXIT_FAILURE);
	}

	/* Connect this socket to remote side */
	sin.sin_family = AF_INET;
	sin.sin_port = htons(22);
	sin.sin_addr.s_addr = inet_addr(hostaddr);
	if (connect(sock, (struct sockaddr*)(&sin),
				sizeof(struct sockaddr_in)) != 0) {
		fprintf(stderr, "Failed to connect\n");
		return (EXIT_FAILURE);
	}

	/* Create a session instance and start it up. This will trade welcome
	 * banners, exchange keys, and setup crypto, compression, and MAC layers */
	session = libssh2_session_init();
	if (!session) {
		fprintf(stderr, "SSH init failed\n");
		return (EXIT_FAILURE);
	}

	/* Handshake for session */
	rc = libssh2_session_handshake(session, sock);
	if (rc) {
		fprintf(stderr, "SSH handshake failed\n");
		return (EXIT_FAILURE);
	}

	/* Lets authenticate */
	rc = libssh2_userauth_password(session, username, password);
	if (rc) {
		printf("Authentication by password failed\n");
		return (EXIT_FAILURE);
	} else {
		printf("Authentication by password succeeded\n");
	}

	/* Request a shell */
	channel = libssh2_channel_open_session(session);
	if (!channel) {
		fprintf(stderr, "Unable to open a session\n");
		return (EXIT_FAILURE);
	}

	/* Request a terminal with 'vanilla' terminal emulation */
	rc = libssh2_channel_request_pty(channel, "vanilla");
	if (rc) {
		fprintf(stderr, "Failed requesting pty\n");
		return (EXIT_FAILURE);
	}

	/* Open a SHELL on that pty */
	rc = libssh2_channel_shell(channel);
	if (rc) {
		fprintf(stderr, "Unable to request shell on allocated pty\n");
		return (EXIT_FAILURE);
	}

	/* Main loop starts here.
	 * In it you will be requested to input a command
	 * command will be executed at remote side
	 * an you will get output from it */
	do {
		/* Request for command input */
		printf("$ ");
		fgets(command, BUFSIZ, stdin);
		printf("Command is %s", command);
		if (strcmp(command, "\n") == 0) {
			printf("Empty command\n");
			continue;
		}

		/* Write command to stdin of remote shell */
		rc = libssh2_channel_write(channel, command, strlen(command));
		printf("Channel write return value is %d\n", rc);

		/* Read output from remote side */
		rc = libssh2_channel_read(channel, inputbuf, BUFSIZ);
		printf("Channel write return value is %d\n", rc);
		printf("Remote side output:\n %s\n", inputbuf);

	} while (strcmp(command, EXIT_COMMAND) != 0);
	/* Main loop ends here */

	/* De-init and pre-exit actions */
	if (channel) {
		libssh2_channel_free(channel);
		channel = NULL;
	}

	rc = libssh2_session_disconnect(session, "Normal Shutdown");
	if (rc) {
		fprintf(stderr, "Session disconnect error\n");
		return (EXIT_FAILURE);
	} else
		printf("Session finished successful\n");

	libssh2_session_free(session);

	close(sock);

	libssh2_exit();

	return (EXIT_SUCCESS);
}
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

Reply via email to