簡體   English   中英

在使用節點的子進程中使用sudo命令

[英]Using sudo command in a child process using node

我正在嘗試將bundle目錄復制到遠程服務器的根目錄中。 我嘗試使用節點執行此操作,到目前為止,我實現了將tar內容傳遞給服務器並解壓縮它。 但是,當我嘗試將目錄移動到根文件夾時,它需要sudo訪問權限,而我卻找不到辦法。 我試過-t選項用於偽終端,但我想這可以從shell運行。 這是我到目前為止所做的,任何幫助都非常感謝:

const path = require("path");
const exec = require('child_process').exec;
var absolutePath = path.resolve(__dirname, "../");
const allCommands = [];
/*
 *
 *
 * 1-) cd to the root folder of the app
 * 2-) tar dist folder and pipe the result to the ssh connection
 * 3-) connect to server with ssh
 * 4-) try to create dist and old_dists folder, if not existing they will be created otherwise they will give an error and rest of the script will continue running
 * 5-) cp contents of dist folder to old_dists/dist_$(dateofmoment) folder so if something is wrong somehow you have an backup of the existing config
 * 6-) untar the piped tar content into dist folder, untar only files under the first parent directory --strip-components=1 flag, if it was 2 it will dive 2 level from the root folder
 *
 *
 */
allCommands.push("cd " + absolutePath);
allCommands.push("tar -czvP dist | ssh  hostnameofmyserver 'mkdir dist ; mkdir old_dists; cp -R dist/ old_dists/dist_$(date +%Y%m%d_%H%M%S) && tar -xzvP -C dist --strip-components=1'");
//I would like to untar the incoming file into /etc/myapp for example rather than my home directory, this requires sudo and don't know how to handle it
exec(allCommands.join(" && "),
  (error, stdout, stderr) => {
    console.log(`stdout: ${stdout}`);
    console.log(`stderr: ${stderr}`);
    if (error !== null) {
      console.log(`exec error: ${error}`);
    }
  });

還有什么是在ubuntu服務器中存儲web應用程序文件夾的最佳位置,其中多個用戶可以部署應用程序,這是一個很好的做法,使目錄root用戶的所有者,或者它無關緊要?

ssh手冊頁中所述,即使OpenSSH客戶端的stdin不是tty(在節點中生成子進程時默認情況下也不會這樣),您可以指定多個-t參數來強制進行pty分配。

從那里,你應該能夠簡單地寫密碼子進程的.stdin當你看到在sudo的提示流.stdout流。

在半相關的說明中,如果您想要對ssh連接進行更多(程序化)控制,或者您不想啟動子進程,則需要ssh2模塊。 如果你願意,你甚至可以在節點內進行tarring,因為npm上還有tar模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM