Blame doc/command.dox

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