How to copy a file in C/C++ with libssh and sftp

sftp in c program
sftp c
libssh vs libssh2
libssh code
libssh documentation
libssh arm support
scp library c++
libopenssh

I'm posting here because I need help with some code relating with libssh.

I read all the official documentation here but still I don't understand the things i need to do if someone can en light me I would be pleased.

Actually I want to copy a file from a client to a remote server but I don't understand how to do it with the library libssh and the function sftp in libssh.

The situation is that: The ssh session is open and the sftp session is open too, I can create a file and write in it from the client to the server with the integrated function of lib ssh.

I did not find an easy way to copy a file from the client to the server with a simple function like sftp_transfer(sourceFile(like c:\my document\hello world.txt),RemoteFile(/home/user/hello world.txt),right(read and write)) ?

With what I have understood from the tutorial it is first creating a file in the remote location (server) then it is opening this file with this line of code :

file = sftp_open(sftp, "/home/helloworld.txt",access_type,1);

After that the file is created on the server , and then it is writing into this created file with a buffer:

const char *helloworld = "Hello, World!\n";
int length = strlen(helloworld);
nwritten = sftp_write(file, helloworld, length);

My question is now if I have a file for example a .doc file and I want to transfer/upload that file from c:\mydocument\document.doc to remote the remote server /home/user/document.doc how can I do it with this method ?

How can I put this file into the sftp_write() function to send it like the helloworld in the sample function ?

I may be not good enough in programming to understand, but I really tried to understand it and i'm stuck with it.

Thanks in advance for your help

See below a sample of the code I used to test:

// Set variable for the communication
      char buffer[256];
      unsigned int nbytes;

        //create a file to send by SFTP
        int access_type = O_WRONLY | O_CREAT | O_TRUNC;
        const char *helloworld = "Hello, World!\n";
        int length = strlen(helloworld);

        //Open a SFTP session
        sftp = sftp_new(my_ssh_session);
        if (sftp == NULL)
        {
            fprintf(stderr, "Error allocating SFTP session: %s\n",
            ssh_get_error(my_ssh_session));
            return SSH_ERROR;
        }
        // Initialize the SFTP session
        rc = sftp_init(sftp);
        if (rc != SSH_OK)
        {
            fprintf(stderr, "Error initializing SFTP session: %s.\n",
            sftp_get_error(sftp));
            sftp_free(sftp);
            return rc;
         }

        //Open the file into the remote side
        file = sftp_open(sftp, "/home/helloworld.txt",access_type,1);
        if (file == NULL)
        {
            fprintf(stderr, "Can't open file for writing: %s\n",ssh_get_error(my_ssh_session));
            return SSH_ERROR;
        }

        //Write the file created with what's into the buffer
        nwritten = sftp_write(file, helloworld, length);
        if (nwritten != length)
        {
            fprintf(stderr, "Can't write data to file: %s\n",
            ssh_get_error(my_ssh_session));
            sftp_close(file);
            return SSH_ERROR;
        }

` 

Open the file in the usual way (using C++'s fstream or C's stdio.h), read its contents to a buffer, and pass the buffer to sftp_write.

Something like this:

ifstream fin("file.doc", ios::binary);
if (fin) {
  fin.seekg(0, ios::end);
  ios::pos_type bufsize = fin.tellg(); // get file size in bytes
  fin.seekg(0); // rewind to beginning of file

  char* buf = new char[bufsize];
  fin.read(buf, bufsize); // read file contents into buffer

  sftp_write(file, buf, bufsize); // write to remote file
}

Note that this is a very simple implementation. You should probably open the remote file in append mode, then write the data in chunks instead of sending single huge blob of data.

How to copy a file in C/C++ with libssh and sftp - c++ - android, Actually I want to copy a file from a client to a remote server but I don't understand how to do it with the library libssh and the function sftp in libssh. The situation  Actually I want to copy a file from a client to a remote server but I don't understand how to do it with the library libssh and the function sftp in libssh. The situation is that: The ssh session is open and the sftp session is open too, I can create a file and write in it from the client to the server with the integrated function of lib ssh.

The following example uses ifstream in a loop, to void loading a whole file into a memory (what the accepted answer does):

ifstream fin("C:\\myfile.zip", ios::binary);

while (fin)
{
    #define MAX_XFER_BUF_SIZE 10240
    char buffer[MAX_XFER_BUF_SIZE];
    fin.read(buffer, sizeof(buffer));
    if (fin.gcount() > 0)
    {
        ssize_t nwritten = sftp_write(NULL, buffer, fin.gcount());
        if (nwritten != fin.gcount())
        {
            fprintf(stderr, "Can't write data to file: %s\n", ssh_get_error(ssh_session));
            sftp_close(file);
            return 1;
        }
    }
}

libssh/samplesftp.c at master · substack/libssh · GitHub, libssh/examples/samplesftp.c. Find file Copy path. Fetching contributors… Cannot retrieve contributors at this time. Actually I want to copy a file from a client to a remote server but I don't understand how to do it with the library libssh and the function sftp in libssh. The situation is that: The ssh session is open and the sftp session is open too, I can create a file and write in it from the client to the server with the integrated function of lib ssh.

I used followed the example here.

sftp_read_sync uses an infinite loop to read a file from a server into /path/to/profile from server path /etc/profile.

Chapter 5: The SFTP subsystem, SFTP stands for "Secure File Transfer Protocol". It enables you to safely transfer files between the local and the remote computer. It reminds a lot of the old FTP  The communication between client and server should be encrypted, there should be an authentication by username and password and the protocol should be a common one, so i thought about sftp. There are two c/c++ libraries for ssh: libssh and libssh2, where only the first one provides also server functionality.

Chapter 6: The SCP subsystem, The SCP subsystem has far less functionality than the SFTP subsystem. However​, if you only need to copy files from and to the remote system, it does its job. +-- file1 +-- B --+ | +-- file2 -- A --+ | +-- file3 +-- C --+ +-- file4. You would do it that  /* * Sample showing how to do SFTP append of a local file onto a remote one. password * and path to copy, but you can specify them on the command line like:

The Tutorial, libssh is a C library that enables you to write a program that uses the SSH protocol. With it, you can remotely execute programs, transfer files, or use a secure  You'll also find all examples in the distribution archive, in the examples/simple directory.examples/simple directory.

Chapter 1: A typical SSH session, Execute a single command. Open a shell. You may want to request a pseudo-​terminal before. Invoke the sftp subsystem to transfer files. Invoke the scp subsystem  This file is part of the SSH Library. You are free to copy this file, modify it in any way, consider it being public. domain. This does not apply to the rest of the library though, but it is. allowed to cut-and-paste working code from this file to any license of. program. The goal is to show the API in action.

Comments
  • Thanks for your fast respond, yeah I was thinking about something like that too. I think it is still low level coding even with this library I don't say it is bad :p lib but you still have to be skilled in C/C++ to understand it and use it properly. I'll try to do something and then post again what I've made later.
  • Use std::vector<char> or std::unique_ptr<char[]> to avoid the memory leak here.
  • @RaphaelTeubner : First i opened and created the file file = sftp_open(sftp, "/home/ra/Desktop/sami/Win32OpenSSL_Light-1_1_0i.exe", O_CREAT, S_IWRITE) then i opened it for writing file = sftp_open(sftp, "/home/ra/Desktop/sami/Win32OpenSSL_Light-1_1_0i.exe", O_WRONLY, S_IWRITE); File is created in the bestination(remote), but the data is not written into the file(File size is 0kb)