writing ardude commands remotely via libssh

69 views Asked by At

I'm writing a code in C++ with libssh and i want to connecting via ssh to Raspberry Pi 3 and remotely writing AVRdude commands in Raspberry. It is possible? Below is my code:

#include <stdio.h>
#include <stdlib.h>
#include <libssh/libssh.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_XFER_BUF_SIZE 16384 

int main()
{
    ssh_session session;
    int verbosity = SSH_LOG_PROTOCOL;
    int port = 22;
    int access_type;
    int nbytes, nwritten, rc;
    int fd;
    int AVR;
    char buffer[MAX_XFER_BUF_SIZE];

    
    session = ssh_new();
    if (session == NULL)
    {
        fprintf(stderr, "Error creating SSH session.\n");
        return 1;
    }

    
    ssh_options_set(session, SSH_OPTIONS_HOST, "IP");
    ssh_options_set(session, SSH_OPTIONS_PORT, &port);
    ssh_options_set(session, SSH_OPTIONS_USER, "ID");
    ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

    
    if (ssh_connect(session) != SSH_OK)
    {
        fprintf(stderr, "Error connecting to Raspberry Pi.\n");
        ssh_free(session);
        return 1;
    }

    
    if (ssh_userauth_password(session, NULL, "PASSWORD") != SSH_AUTH_SUCCESS)
    {
        fprintf(stderr, "Error authenticating.\n");
        ssh_disconnect(session);
        ssh_free(session);
        return 1;
    }

  ssh_channel channel;
  

  channel = ssh_channel_new(session);
  if (channel == NULL){ 
  return SSH_ERROR;
  }
  rc = ssh_channel_open_session(channel);
  if (rc != SSH_OK)
  {
    ssh_channel_free(channel);
    return rc;
}

fd = ssh_channel_request_exec(channel, "sudo avrdude");
if (fd > 0) {
    return -1;
}
 
while ((fd = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
    if (fwrite(buffer, 1, fd, stdout) != (unsigned int) fd) {
        return -1;
    }
}

    
    ssh_disconnect(session);
    ssh_free(session);

    return 0;
}

When i run the code nothing happens. It works with other commands (with and without sudo).

I've tried changing permissions and editing system environment variables, but nothing happened.

0

There are 0 answers