2017-01-03 81 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)); 

免責聲明:我這個模塊的作者

相關問題