簡體   English   中英

使用 ssh2shell node.js 為多個設備執行多個命令

[英]Execute multiple commands for multiple devices using ssh2shell node.js

在我的項目中,我有一組設備 ip 和一組命令。 I need to open new ssh shell for each devcie, that executes all the commands in the same shell, so it won't open new connection for each command, and that's why I'm using ssh2shell package.

問題是我需要等待所有這些都結束,並且它需要是異步的,因此它不會等待每個 ip 的答案,然后再與其他設備一起啟動 session。 在獲得所有響應后,它將所有數據保存在某個數組中,該數組中的每個單元格代表一個 ip 響應。

我知道它需要使用asyncawaitPromise ,但我沒有成功管理它。

主要代碼:

function monitorDevices() {
    
    const DEVICES_IPS = [ "10.10.10.1", "20.20.20.1", "30.30.30.1" ];
    const COMMANDS    = [ "cat /var/log/messages", "cat /proc/stat", "df -h /var/log", "free -t -m" ];
    let data = [];
    DEVICES_IPS.forEach(device => {
       data.push(sshService.exec(device, COMMANDS));
    });

    console.log(data);

}

ssh服務代碼:

function exec(device, commands) {

    var host = {
     server: {     
      host:      device,
      userName:  process.env.USERNAME,
      password:  process.env.PASSWORD,
     },
     commands:      commands
    };
     
    var SSH2Shell = require ('ssh2shell'),
      SSH = new SSH2Shell(host),
      callback = function(sessionText){
        // if I console.log(sessionText) here, it gives my a good result.
        return sessionText;
      }

    SSH.connect(callback);

}

謝謝!

這是我將如何承諾包裝SSH2ShellSSH.connect -

const SSH2Shell = require ('ssh2shell')

function exec(device, commands) {
  const SSH = new SSH2Shell({ 
    server: {     
      host: device,
      userName: process.env.USERNAME,
      password: process.env.PASSWORD,
    },
    commands: commands
  })
  return new Promise(resolve => SSH.connect(resolve))
}

現在我們將monitorDevices編寫為async function。 只需遍歷devicesawait每個sshService.exec並將其push送到data結果 -

async function monitorDevices() {
  const devices =
    [ "10.10.10.1", "20.20.20.1", "30.30.30.1" ]

  const commands =
    [ "cat /var/log/messages", 
      "cat /proc/stat", 
      "df -h /var/log", 
      "free -t -m"
    ]

  const data = []
  for (const d of devices)
    data.push(await sshService.exec(d, commands))
  return data
}

要運行 function,我們編寫 -

monitorDevices().then(console.log, console.error)

我的建議是制作monitorDevices function 的devicescommands參數 -

async function monitorDevices(devices, commands) {  // parameters
  const data = []
  for (const d of devices)
    data.push(await sshService.exec(d, commands))
  return data
}

現在它可用於各種設備和命令 -

monitorDevices
  ( [ "10.10.10.1", ... ]              // devices
  , [ "cat /var/log/messages",  ... ]  // commands
  )
  .then(console.log, console.error)

最后,我們可能希望讓我們的exec function 更加健壯一些。 我們可以通過連接到主機的onError屬性來添加一些錯誤處理 -

function exec(device, commands) {
  return new Promise((resolve, reject) => 
    new SSH2Shell({ 
      server: {     
        host: device,
        userName: process.env.USERNAME,
        password: process.env.PASSWORD,
      },
      commands: commands,
      callback: resolve,    // <- resolve callback
      onError: reject       // <- reject error
    })
}

暫無
暫無

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

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