Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a file with SFTP using an ASC file and username/password in NodeJS

I've been working on a task to upload a file to a partner's ftp site using a public PGP key in an asc file he sent me. The file looks like this (with the bulk of the key censored out):

      -----BEGIN PGP PUBLIC KEY BLOCK-----
      Version: PGP Desktop 10.2.1 (Build 4461)

      mQEN... ... ...=K8zL
      -----END PGP PUBLIC KEY BLOCK-----

I have tried several SFTP node packages, but nothing seems to be working yet. Most of the examples use a .rsa file, but all I have provided is this .asc file. From my research, I can see that some people have used files with this extension, but nobody has fully explained how.

I am able to connect to the FTP site with Filezilla and get a message that the hostkey algorithm is ssh-dss 1024 along with SHA256 and MD5 fingerprints. I'm not sure if that's helpful or not.

Does anybody have experience with .asc files and how they are used to establish an SFTP connection?

EDIT: I've tried using the npm package sftp-upload with the following code:

    var SftpUpload = require('sftp-upload'), fs = require('fs');
    var options = {
        host:'ftp.partnersite.com',
        username:'TempUserName',
        path: './CSV',
        remoteDir: '/',
        privateKey: fs.readFileSync('pgpkeyfile.asc'),
    },
    sftp = new SftpUpload(options);

    sftp.on('error', function(err) {
        throw err;
    })
    .on('uploading', function(progress) {
        console.log('Uploading', progress.file);
        console.log(progress.percent+'% completed');
    })
    .on('completed', function() {
        console.log('Upload Completed');
    })
    .upload();

On running the code, I got the following error: Error: Unable to parse given privateKey value which I assumes means this keyfile is in the incorrect format for sftp_upload.

like image 694
Rob Avatar asked Oct 03 '17 21:10

Rob


People also ask

What is SSH2 SFTP client?

Overview. an SFTP client for node. js, a wrapper around SSH2 which provides a high level convenience abstraction as well as a Promise based API. Documentation on the methods and available options in the underlying modules can be found on the SSH2 project pages. Current stable release is v9.


1 Answers

As it turns out, the ASC file was not needed for the SFTP connection itself, but rather only used for encrypting the file before sending it. I was able to use the node package ssh2-sftp-client to connect to the FTP site and upload a file with the following code:

    sftp.connect({
        host: 'ftp.site.com',
        port: '22',
        username: 'myusername',
        password: 'mypassword',
        algorithms: {
          serverHostKey: [ 'ssh-dss' ],
          kex: ['diffie-hellman-group14-sha1'],
          cipher: ['aes128-cbc']
        }
    }).then(() => {
        sftp.put('./CSV/myspreadsheet.csv', '/myspreadsheet.csv', false);
    }).then((data) => {
        console.log(data, 'the data info');
    }).catch((err) => {
        console.log(err, 'catch error');
    });
like image 168
Rob Avatar answered Nov 15 '22 06:11

Rob