0

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.

  • If it works with other commands but not with this one you need to debug what happens on the remote side. Try starting `sudo strace -f avrdude` instead to trace all system calls. – Botje Aug 28 '23 at 13:07
  • What do you expect? `avrdude` without parameters prints help message to stderr and exits. You are trying to read its stdout: last parameter of `ssh_channel_read()` is 0. – dimich Aug 28 '23 at 20:32
  • Botje - what exactly should i look for after typing this command? dimich - the code is to be used to read data from the memory of the ATmega microcontroller or to write it. For now, I would like to know if this is possible and if so, what would I need to change. – adam1234_5341 Aug 29 '23 at 08:57
  • Ok I changed 0 to 1 and it worked `while ((fd = ssh_channel_read(channel, buffer, sizeof(buffer), 1)) > 0)`. the commands work but it does not write data from memory in binary or hexadecimal versions. Any ideas? – adam1234_5341 Aug 29 '23 at 09:59
  • @adam1234_5341 What exact command do you execute? Does it work when run from command line via ssh? *Does not write data* - how do you know this? What is output? – dimich Aug 29 '23 at 10:35
  • at the same time I check the commands on the RPI and typing, for example, the command "sudo avrdude -p ATmega168 -C ~/avrdude_gpio_conf -c pi_1 -U lfuse:r:-:i" on the RPI I get "avrdude: writing output file :01000000FD02 :000000001FF", but in PC "avrdude: writing output file ". When it comes to, for example, saving a data file in RPI, it works fine, but I'm looking for a solution that will allow me to display data in the console. – adam1234_5341 Aug 29 '23 at 10:49
  • @adam1234_5341 The message *avrdude: writing output file * is written to stderr. *:01000000FD02 :000000001FF* is written to stdout. In your code you read either stdout or stderr. You have to read both. Another option is to redirect one stream to another in the command itself, e.g. stderr to stdout: `avrdude ... 2>&1`, and read stdout only. But in this case you can't distinguish diagnostic messages from data output. – dimich Aug 29 '23 at 13:25

0 Answers0