2017-10-17 526 views
3

我想寫一個npm腳本來執行ssh shell命令。目前它正在通過執行osascript命令來打開終端窗口並運行該命令。從npm命令執行ssh shell腳本

我想改變它來執行當前終端的命令。該腳本包括shelljsexecutive。當我使用executive時,腳本沒有發生任何事情。隨着shelljs我得到:

Pseudo-terminal will not be allocated because stdin is not a terminal. the input device is not a TTY

正在執行的命令是:ssh -i [ssh-key] -t [email protected][ip-address] eval $(base64 -D <<< [command in base64])

基於64位指令sudo docker exec -i -t $(sudo docker ps -aqf "ancestor=' + containerName + '") /bin/bash

如果我輸出的命令和複製和粘貼,因爲預期它會工作,進入遠程機器並運行docker exec命令。

如果我刪除-t選項,我不會收到警告消息,但控制檯中沒有輸出並且腳本掛起(我假設它在後臺運行沒有輸出的命令)。如果我刪除eval ...部分,我會得到一個輸出,看起來像您在切換到服務器時看到的內容,但沒有輸入終端。

我該怎麼做才能在同一個終端或新選項卡中執行此命令。如果我必須使用osascript命令來執行此操作,那也沒關係。不過我會從PhpStorm的終端執行這個命令。

編輯

這裏的代碼塊:

var execCommand = 'sudo docker exec -i -t $(sudo docker ps -aqf "ancestor=nginx") /bin/bash'; 

var buffer = new Buffer(execCommand); 
var encoded = buffer.toString('base64'); 

var cmd = "ssh -i " + this.keyPath + " -t [email protected]" + ip + " eval $(base64 -D <<< " + encoded + ") "; 

shell.exec(cmd); 

編輯2

我可以ssh到機器成功,並得到一個命令提示符,但我得到一個當我添加eval命令時,現在出現the input device is not a TTY錯誤。

var docker_exec = 'sudo docker exec -it $(sudo docker ps -aqf "ancestor=' + containerName + '") /bin/bash'; 
var encoded = new Buffer(docker_exec).toString('base64'); 

var sshTerm = spawn('ssh', [ 
    '-i', 
    this.keyPath, 
    '[email protected]' + ip, 
    'eval', 
    'eval $(base64 -D <<< ' + encoded + ')' 
], { 
    stdio: 'inherit', 
    shell: true 
}); 

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

您可以發佈您使用的代碼嗎? –

+0

@TarunLalwani希望可以幫助 – Ethan22

回答

1

我檢查過它,它的shelljs.exec非TTY命令非常好。對於基於TTY的命令,您可以使用正常的派生方法。下面是一個很適合我的示例代碼

var spawn = require('child_process').spawn; 

var sshTerm = spawn('ssh', ["[email protected]", ""], { 
    stdio: 'inherit' 
}); 

// listen for the 'exit' event 
// which fires when the process exits 
sshTerm.on('exit', function(code, signal) { 
    if (code === 0) { 
    // process completed successfully 
    } else { 
    // handle error 
    } 
}); 
+0

感謝您的回覆,我會在今天晚些時候對此進行測試並回復您! – Ethan22

+0

這是一個很大的幫助,但我得到了另一個錯誤。你能看看我的編輯2,看看你的想法嗎? – Ethan22

+0

嘗試在''ubuntu @'+ ip之前添加''-tt',',' –