Node.js 查询远程服务器';s操作系统

Node.js 查询远程服务器';s操作系统,node.js,shell,ssh,ssh2-sftp,ssh2-exec,Node.js,Shell,Ssh,Ssh2 Sftp,Ssh2 Exec,我正在Node.js中编写一个微服务,它运行一个特定的命令行操作来获取特定的信息。该服务在多台服务器上运行,其中一些在Linux上,一些在Windows上。我正在使用ssh2exec连接到服务器并执行命令,但是,我需要一种方法来确定服务器的操作系统以运行正确的命令 let ssh2Connect = require('ssh2-connect'); let ssh2Exec = require('ssh2-exec'); ssh2Connect(config, function(error,

我正在Node.js中编写一个微服务,它运行一个特定的命令行操作来获取特定的信息。该服务在多台服务器上运行,其中一些在Linux上,一些在Windows上。我正在使用
ssh2exec
连接到服务器并执行命令,但是,我需要一种方法来确定服务器的操作系统以运行正确的命令

let ssh2Connect = require('ssh2-connect');
let ssh2Exec = require('ssh2-exec');

ssh2Connect(config, function(error, connection) {
    let process = ssh2Exec({
        cmd: '<CHANGE THE COMMAND BASED ON OS>',
        ssh: connection
    });
    //using the results of process...
});
让ssh2Connect=require('ssh2-connect');
设ssh2Exec=require('ssh2-exec');
ssh2Connect(配置,功能(错误,连接){
let进程=ssh2Exec({
cmd:“”,
ssh:连接
});
//使用过程的结果。。。
});

我有一个解决方案的想法:接下来,预先运行一些其他命令,并根据所述命令的输出确定操作系统;然而,我想知道是否有一种更“正式”的方法来实现这一点,特别是使用
SSH2
库。

下面是我认为应该如何做到的。。。 //导入操作系统模块这将允许您读取应用程序正在运行的操作系统类型 const os=要求('os')


你能控制服务器吗?最简单的方法是在每台服务器上安装一个特定于操作系统的脚本,这样无论远程服务器的操作系统如何,客户端都可以运行一个固定的命令。
//define windows os in string there is only one but for consistency sake we will leave it in an array *if it changes in the future makes it a bit easier to add to an array the remainder of the code doesn't need to change
const winRMOS = ['win32']

//define OS' that need to use ssh protocol *see note above
const sshOS = ['darwin', 'linux', 'freebsd']

// ssh function
const ssh2Connect = (config, function(error, connection) => {
let process = ssh2Exec({
    if (os.platform === 'darwin') {
    cmd: 'Some macOS command'
    },
    if (os.platform === 'linux') {
    cmd: 'Some linux command'
    },
    ssh: connection
 });
 //using the results of process...
 });

 // winrm function there may but some other way to do this but winrm is the way i know how
const winRM2Connect = (config, function(error, connection) => {
let process = ssh2Exec({
    cmd: 'Some Windows command'
    winRM: connection
});
//using the results of process...
});


// if statements to determine which one to use based on the os.platform that is returned.
if (os.platform().includes(sshOS)){
  ssh2Connect(config)
} elseif( os.platform().includes(winrmOS)){
  winrm2Connect(config)
}