0

I am trying to send a file from windows machine to linux server using ssh2 in JavaScript, sftp is not working so I am using exec and specifying scp command , but i am getting below error.

STDERR: ssh: Could not resolve hostname C: Name or service not known

var Connection = require('ssh2').Client;
    var c = new Connection();

    c.on('connect', function () {
        console.log('Connection :: connect');
    });
    c.on('ready', function () {
        console.log('Connection :: ready');
       c.exec('scp -r C:/myFolder/133.DAT serverFilePath', function(err, stream) {
                if (err) throw err;
                stream.on('close', function(code, signal) {
                  console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
                  c.end();
                }).on('data', function(data) {
                  console.log('STDOUT: ' + data);
                }).stderr.on('data', function(data) {
                  console.log('STDERR: ' + data);
                });
          });


    });
    c.on('error', function (err) {
        console.log('Connection :: error :: ' + err);
    });
    c.on('end', function () {
        console.log('Connection :: end');
    });
    c.on('close', function (had_error) {
        console.log('Connection :: close');
    });
    c.on('keyboard-interactive', function (name, instructions, instructionsLang, prompts, finish) {
        console.log('Connection :: keyboard-interactive');
        finish(['password']);
    });
    c.connect({
        host: 'hostname',
        // type: 'sftp',
        port: 22,
        username: 'username',
        password: 'pwd',
        readyTimeout: 99999,
        tryKeyboard: true,
        debug: console.log,

    }).then(()=>{
        console.log('Something to print');

    });
Umesh Kumar
  • 1,387
  • 2
  • 16
  • 34

1 Answers1

1

The scp syntax to copy file1 from host1 to the directory todir on the local machine is scp host1:/path/to/file1 todir. This to make it clearer that C: in C:/myFolder/133.DAT is understood as the source hostname rather than a hard drive identifier. Thus the error: the system cannot resolve C as a hostname.

Now, how to specify the a file name located on drive C: depends where your code is running. If your JS is running natively on Windows, see the documentation of the scp implementation you are using. If you are running on cygwin, try /cygdrive/c/path/to/file.

mszmurlo
  • 1,250
  • 1
  • 13
  • 28