www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples | github

Archive Index This month's Index

Subject: How to get about command execution finished on remote side

How to get about command execution finished on remote side

From: Ivan Tretyakov <ivan.v.tretyakov_at_gmail.com>
Date: Mon, 16 Jan 2012 00:09:36 +0400


I try to write a small simple ssh client program to learn how to work with
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");

/* 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

Ivan Tretyakov

libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

Received on 2012-01-15

the libssh2 team