Blame doc/command.dox

Packit Service 31306d
/**
Packit Service 31306d
@page libssh_tutor_command Chapter 4: Passing a remote command
Packit Service 31306d
@section remote_command Passing a remote command
Packit Service 31306d
Packit Service 31306d
Previous chapter has shown how to open a full shell session, with an attached
Packit Service 31306d
terminal or not. If you only need to execute a command on the remote end,
Packit Service 31306d
you don't need all that complexity.
Packit Service 31306d
Packit Service 31306d
The method described here is suited for executing only one remote command.
Packit Service 31306d
If you need to issue several commands in a row, you should consider using
Packit Service 31306d
a non-interactive remote shell, as explained in previous chapter.
Packit Service 31306d
Packit Service 31306d
@see shell
Packit Service 31306d
Packit Service 31306d
Packit Service 31306d
@subsection exec_remote Executing a remote command
Packit Service 31306d
Packit Service 31306d
The first steps for executing a remote command are identical to those
Packit Service 31306d
for opening remote shells. You first need a SSH channel, and then
Packit Service 31306d
a SSH session that uses this channel:
Packit Service 31306d
Packit Service 31306d
@code
Packit Service 31306d
int show_remote_files(ssh_session session)
Packit Service 31306d
{
Packit Service 31306d
  ssh_channel channel;
Packit Service 31306d
  int rc;
Packit Service 31306d
Packit Service 31306d
  channel = ssh_channel_new(session);
Packit Service 31306d
  if (channel == NULL) return SSH_ERROR;
Packit Service 31306d
Packit Service 31306d
  rc = ssh_channel_open_session(channel);
Packit Service 31306d
  if (rc != SSH_OK)
Packit Service 31306d
  {
Packit Service 31306d
    ssh_channel_free(channel);
Packit Service 31306d
    return rc;
Packit Service 31306d
  }
Packit Service 31306d
@endcode
Packit Service 31306d
Packit Service 31306d
Once a session is open, you can start the remote command with
Packit Service 31306d
ssh_channel_request_exec():
Packit Service 31306d
Packit Service 31306d
@code
Packit Service 31306d
  rc = ssh_channel_request_exec(channel, "ls -l");
Packit Service 31306d
  if (rc != SSH_OK)
Packit Service 31306d
  {
Packit Service 31306d
    ssh_channel_close(channel);
Packit Service 31306d
    ssh_channel_free(channel);
Packit Service 31306d
    return rc;
Packit Service 31306d
  }
Packit Service 31306d
@endcode
Packit Service 31306d
Packit Service 31306d
If the remote command displays data, you get them with ssh_channel_read().
Packit Service 31306d
This function returns the number of bytes read. If there is no more
Packit Service 31306d
data to read on the channel, this function returns 0, and you can go to next step.
Packit Service 31306d
If an error has been encountered, it returns a negative value:
Packit Service 31306d
Packit Service 31306d
@code
Packit Service 31306d
  char buffer[256];
Packit Service 31306d
  int nbytes;
Packit Service 31306d
Packit Service 31306d
  nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
Packit Service 31306d
  while (nbytes > 0)
Packit Service 31306d
  {
Packit Service 31306d
    if (fwrite(buffer, 1, nbytes, stdout) != nbytes)
Packit Service 31306d
    {
Packit Service 31306d
      ssh_channel_close(channel);
Packit Service 31306d
      ssh_channel_free(channel);
Packit Service 31306d
      return SSH_ERROR;
Packit Service 31306d
    }
Packit Service 31306d
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
Packit Service 31306d
  }
Packit Service 31306d
Packit Service 31306d
  if (nbytes < 0)
Packit Service 31306d
  {
Packit Service 31306d
    ssh_channel_close(channel);
Packit Service 31306d
    ssh_channel_free(channel);
Packit Service 31306d
    return SSH_ERROR;
Packit Service 31306d
  }
Packit Service 31306d
@endcode
Packit Service 31306d
Packit Service 31306d
Once you read the result of the remote command, you send an
Packit Service 31306d
end-of-file to the channel, close it, and free the memory
Packit Service 31306d
that it used:
Packit Service 31306d
Packit Service 31306d
@code
Packit Service 31306d
  ssh_channel_send_eof(channel);
Packit Service 31306d
  ssh_channel_close(channel);
Packit Service 31306d
  ssh_channel_free(channel);
Packit Service 31306d
Packit Service 31306d
  return SSH_OK;
Packit Service 31306d
}
Packit Service 31306d
@endcode
Packit Service 31306d
Packit Service 31306d
*/