我正在尝试使用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');
});
scp
语法将file1
从host1
复制到本地计算机上的目录todir
是scp host1:/path/to/file1 todir
。这可以清楚地表明,C:/myFolder/133.DAT
中的C:
被理解为源主机名,而不是硬盘驱动器标识符。因此,错误:系统无法将C
解析为主机名。
现在,如何指定驱动器C:
上的文件名取决于您的代码在何处。如果您的JS在Windows上本地运行,请参见您正在使用的SCP实现的文档。如果您在Cygwin上运行,请尝试/cygdrive/c/path/to/file
。