2017-01-03 74 views
1

我目前正在将我们的内部CLI工具重建为命令行节点应用程序。其中一部分涉及重建bash脚本以SSH进入该应用的特定服务器部分。从节点脚本打开交互式SSH会话

我知道如何使用child_processspawn功能实际执行SSH,但是这并不能产生相同的结果,只是在外壳SSH'ing直接(甚至在ssh命令标志使用-tt时)。例如,键入的命令会在屏幕上显示两次,并且在这些远程计算机上尝试使用nano根本不起作用(屏幕尺寸不正确,仅占用控制台窗口的大约一半,并且使用箭头不起作用)。

有没有更好的方式在节点应用程序中做到这一点?这是一般的代码我目前使用的启动SSH会话:

run: function(cmd, args, output) { 
    var spawn = require('child_process').spawn, 
     ls = spawn(cmd, args); 

    ls.stdout.on('data', function(data) { 
     console.log(data.toString()); 
    }); 

    ls.stderr.on('data', function(data) { 
     output.err(data.toString()); 
    }); 

    ls.on('exit', function(code) { 
     process.exit(code); 
    }); 

    process.stdin.resume(); 
    process.stdin.on('data', function(chunk) { 
     ls.stdin.write(chunk); 
    }); 

    process.on('SIGINT', function() { 
     process.exit(0); 
    }); 
} 
+0

我做了一个该模块[ssh2-client](https://github.com/MatthieuLemoine/ssh2-client) – MatthieuLemoine

回答

1

可以使用ssh2-client

const ssh = require('ssh2-client'); 

const HOST = '[email protected]'; 

// Exec commands on remote host over ssh 
ssh 
    .exec(HOST, 'touch junk') 
    .then(() => ssh.exec(HOST, 'ls -l junk')) 
    .then((output) => { 
    const { out, error } = output; 
    console.log(out); 
    console.error(error); 
    }) 
    .catch(err => console.error(err)); 

// Setup a live shell on remote host 
ssh 
    .shell(HOST) 
    .then(() => console.log('Done')) 
    .catch(err => console.error(err)); 

免责声明:我这个模块的作者