How to run a program in $HOME/[.local/]bin folder with libssh on Linux/Unix in C with libssh

61 views Asked by At

I have a doubt. Is it possible to run a program in the $HOME/[.local/]bin directory with libssh without identifying the path?

Imagine the following situation: You need to install and run software on a remote node without root privileges on a unix-like system.

How to run a program installed in the user area in one of the following paths $HOME/[.local/]bin or $HOME/bin in a C program with libssh, similar to using the command with the following arguments ssh [email protected]. 1.2 $SHELL -l -c "foo &".

This path is already defined in the user profile when logging in.

I can run it with the following line in the code:

rc = ssh_channel_request_exec(channel, "cd /home/my_user/.local/bin/; ./foo &");

But I would like to replace the above function call with this:

rc = ssh_channel_request_exec(channel, "foo &");

Is it possible? If is possible, what needs to be done to be able to run the program this way?

My example is as follows:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>

void free_channel(ssh_channel channel) {
    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
}

void free_session(ssh_session session) {
    ssh_disconnect(session);
    ssh_free(session);
}

void error(ssh_session session) {
    fprintf(stderr, "Error: %s\n", ssh_get_error(session));
    free_session(session);
    exit(-1);
}

int main()
{
    ssh_session session;
    ssh_channel channel;
    int rc, port = 22;
    char buffer[1024];
    unsigned int nbytes;

    printf("Session...\n");
    session = ssh_new();
    if (session == NULL) exit(-1);

    ssh_options_set(session, SSH_OPTIONS_HOST, "192.168.1.2");
    ssh_options_set(session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(session, SSH_OPTIONS_USER, "user01");

    printf("Connecting...\n");
    rc = ssh_connect(session);
    if (rc != SSH_OK) error(session);

    printf("Publickey Autentication...\n");
    rc = ssh_userauth_publickey_auto(session, NULL, NULL);
    if (rc != SSH_AUTH_SUCCESS) error(session);

    printf("Channel...\n");
    channel = ssh_channel_new(session);
    if (channel == NULL) exit(-1);

    printf("Opening...\n");
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK) error(session);

    printf("Executing remote command...\n");
    rc = ssh_channel_request_exec(channel, "cd /home/user01/.local/bin/; ./foo &");

    if (rc != SSH_OK) error(session);

    printf("Received:\n");
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0) {
        fwrite(buffer, 1, nbytes, stdout);
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    }

    free_channel(channel);
    free_session(session);
 
    return 0;
}

Thanks Denilson Bélo

I tried to replace the part of the code ssh_channel_write with ssh_channel_write, but I didn't get the desired result.

0

There are 0 answers