首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询远程服务器的操作系统

查询远程服务器的操作系统
EN

Stack Overflow用户
提问于 2019-05-18 04:15:25
回答 1查看 183关注 0票数 5

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

代码语言:javascript
复制
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...
});

我有一个解决方案的想法:遵循this question,预先运行一些其他命令,并根据该命令的输出确定操作系统;但是,我想了解是否有更“正式”的方法来实现这一点,特别是使用SSH2库。

EN

回答 1

Stack Overflow用户

发布于 2021-06-15 00:46:57

下面是我想要做的事情。//导入os模块这将允许您读取应用程序在其上运行的os类型const os = require('os');

代码语言:javascript
复制
//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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56193018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档