使用ssh2:ssh从Windows到Linux的文件传输失败:无法解析主机名C:名称或服务未知

问题描述 投票:0回答:1

我试图在JavaScript中使用ssh2将文件从Windows机器发送到linux服务器,sftp无效,所以我使用exec并指定scp命令,但我得到的错误。

STDERR:ssh:无法解析主机名C:名称或服务未知

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');

    });
javascript node.js unix scp ssh2
1个回答
0
投票

scpfile1复制到本地机器上host1目录的todir语法是scp host1:/path/to/file1 todir。这使得更清楚的是C:中的C:/myFolder/133.DAT被理解为源主机名而不是硬盘驱动器标识符。因此错误:系统无法将C解析为主机名。

现在,如何指定驱动器C:上的文件名取决于代码运行的位置。如果您的JS在Windows上本机运行,请参阅您正在使用的scp实现的文档。如果你在cygwin上运行,请尝试/cygdrive/c/path/to/file

© www.soinside.com 2019 - 2024. All rights reserved.