On Wednesday 29 February 2012 06:43:36 you wrote:
> So I wrote a function that takes a channel, command and filename and runs a
> ssh_channel_remote_exec on the channel with the command, and dumps the
> output in the given file.
>
> int ssh_library_remote_cmd_to_file(ssh_channel channel, const char *cmd,
> const char *filename)
> {
> char buffer[256];
> int nbytes;
> int rc;
>
> if (!filename || !cmd || !ssh_channel_is_open(channel))
> return SSH_ERROR;
>
> rc = ssh_channel_request_exec(channel, cmd);
> if (rc == SSH_OK) {
> FILE *fp = fopen(filename, "w");
>
> if (!fp)
> return SSH_ERROR;
>
> nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
>
> while (nbytes > 0 && ssh_channel_is_open(channel) &&
> nbytes != SSH_ERROR && !ssh_channel_is_eof(channel))
> {
> write(1, buffer, nbytes);
> fwrite(buffer, sizeof(buffer[0]),
> sizeof(buffer)/sizeof(buffer[0]),$ nbytes = ssh_channel_read(channel,
> buffer, sizeof(buffer), 0); }
> fclose(fp);
> if (nbytes < 0)
> return SSH_ERROR;
> }
> return SSH_OK;
> }
>
>
> It works fine the first time it is called, but does not work when called a
> second time. I have tried using the same channel, using different channels
> that are part of the same session, and using different channels on
> different sessions. The last instance really surprises me.
After you have successfully executed the command you have to close the
channel. See our tutorial at:
http://api.libssh.org/stable/libssh_tutor_command.html
Cheers,
-- andreas
--
Andreas Schneider GPG-ID: F33E3FC6
www.cryptomilk.org [email protected]