[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

question on non interactive execution


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.

Can anyone offer any advice on what to change so that I can get this function to work appropriately on multiple calls? On secondary calls, ssh_channel_request_exec(channel, cmd) does not return SSH_OK.

Any help would be appreciated.

Nan

Follow-Ups:
Re: question on non interactive executionGearoid Murphy <gearoid.murphy@xxxxxx>
Re: question on non interactive executionAndreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org