Centos Ubuntu RedOS系统类型下查看系统信息

在这里插入图片描述

文章目录

  • 一、项目背景
  • 二、页面
  • 三、说明
  • 四、代码
    • 1.SysInfo
    • 2.EmsSysConfig
    • 3.HostInformationController
    • 4.HostInfo

一、项目背景

公司项目想展示当前部署系统的:操作系统,软件版本、IP、主机名。

二、页面

在这里插入图片描述

三、说明

说明点1:查询系统类型及IP实际流程是,程序执行linux命令获取结果信息的过程。

说明点2:查询系统类型命令

hostnamectl | awk -F': +' '/Static hostname|Operating System/ {printf "%s,", $2}' | sed 's/,$//'

说明点3:目前项目支持查询3种不同类型的系统:Centos、Ubuntu、Res OS

说明点4:其中ip、os和hostname是从服务器查询出来的;version是从配置文件或者适配包读取的;

说明点5:IP其实是从网络接口中获取的信息,比如linux执行iFconfig即可获取网络接口信息,在每个接口的输出中,inet 行表示该接口的 IPv4 地址,inet6 行表示 IPv6 地址

CentOS 系统

  • 常见命名
    • 传统上使用 ethX(如 eth0, eth1)
    • 新版本(如 CentOS 7 及以上)可能使用 enoX 或 ensX 这样的命名方式。

Ubuntu 系统

  • 常见命名:
    • 新版本的 Ubuntu 通常使用 enpX(如 enp0s3, enp0s8)。

Red Hat 系统

  • 常见命名:
    • 新版本的 Red Hat 和 Fedora 系统通常使用 ensX(如 ens33, ens160)。

结论:

  • 对于 CentOS,你可以检查 eth, eno, 和 ens 开头的接口。
  • 对于 Ubuntu,检查 enp 开头的接口。
  • 对于 Red Hat 和 Fedora,检查 ens 开头的接口。

这种方式可以确保你能够捕获到常见的网络接口命名,并获取相应的 IPv4 地址。

四、代码

1.SysInfo

package com.hero.lte.ems.sysconf.model;import javax.xml.bind.annotation.XmlAttribute;/*** ems系统基本信息** @author w17231* @date 2017年4月13日*/
public class SysInfo {/*** ems对外通信IP*/private String emsCommIp;/*** ems系统IP*/private String emsSysIp;/*** 系统语言*/private String locale;/*** 产品名称*/private String productName;/*** 进程名称*/private String processName;/*** 产品版本*/private String productVersion;private String ftpRootPath;private String ftpUploadPath;private String verification;private String ftpPatchPath;/*** 制造商*/private String manufacturer;/*** 产品描述*/private String description;private String systemVersion;private String systemRoot;public String getEmsCommIp() {return emsCommIp;}public void setEmsCommIp(String emsCommIp) {this.emsCommIp = emsCommIp;}public String getEmsSysIp() {return emsSysIp;}public void setEmsSysIp(String emsSysIp) {this.emsSysIp = emsSysIp;}public String getLocale() {return locale;}public void setLocale(String locale) {this.locale = locale;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}@XmlAttributepublic String getProductVersion() {return productVersion;}public void setProductVersion(String productVersion) {this.productVersion = productVersion;}public String getManufacturer() {return manufacturer;}public void setManufacturer(String manufacturer) {this.manufacturer = manufacturer;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public String getProcessName() {return processName;}public void setProcessName(String processName) {this.processName = processName;}public String getFtpRootPath() {return ftpRootPath;}public void setFtpRootPath(String ftpRootPath) {this.ftpRootPath = ftpRootPath;}public String getFtpUploadPath() {return ftpUploadPath;}public void setFtpUploadPath(String ftpUploadPath) {this.ftpUploadPath = ftpUploadPath;}public String getVerification() {return verification;}public void setVerification(String verification) {this.verification = verification;}public String getSystemVersion() {return systemVersion;}public void setSystemVersion(String systemVersion) {this.systemVersion = systemVersion;}public String getSystemRoot() {return systemRoot;}public void setSystemRoot(String systemRoot) {this.systemRoot = systemRoot;}public String getFtpPatchPath() {return ftpPatchPath;}public void setFtpPatchPath(String ftpPatchPath) {this.ftpPatchPath = ftpPatchPath;}@Overridepublic String toString() {return "SysInfo{" +"emsCommIp='" + emsCommIp + '\'' +", emsSysIp='" + emsSysIp + '\'' +", locale='" + locale + '\'' +", productName='" + productName + '\'' +", processName='" + processName + '\'' +", productVersion='" + productVersion + '\'' +", ftpRootPath='" + ftpRootPath + '\'' +", ftpUploadPath='" + ftpUploadPath + '\'' +", ftpPatchPath='" + ftpPatchPath + '\'' +", verification='" + verification + '\'' +", manufacturer='" + manufacturer + '\'' +", description='" + description + '\'' +", systemVersion='" + systemVersion + '\'' +", systemRoot='" + systemRoot + '\'' +'}';}
}

2.EmsSysConfig

package com.hero.lte.ems.configuration;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hero.lte.ems.sysconf.model.InstallInfo;
import com.hero.lte.ems.sysconf.model.SysInfo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;@Configuration
public class EmsSysConfig {@Bean(name = "sysInfo")public SysInfo getSysInfo() {SysInfo sysInfo = new SysInfo();DynamicConfig dynamicConfig = DynamicConfigLoader.load("server.properties");sysInfo.setEmsCommIp(dynamicConfig.getString("system.transport.ip"));sysInfo.setEmsSysIp(dynamicConfig.getString("service.host"));sysInfo.setProcessName(dynamicConfig.getString("service.name"));sysInfo.setProductName(dynamicConfig.getString("service.productName"));sysInfo.setSystemVersion(dynamicConfig.getString("service.version"));sysInfo.setFtpRootPath(dynamicConfig.getString("system.ftprootpath"));sysInfo.setFtpUploadPath(dynamicConfig.getString("system.uploadpath"));sysInfo.setFtpPatchPath(dynamicConfig.getString("system.ftppatchpath"));sysInfo.setLocale(dynamicConfig.getString("system.language"));sysInfo.setVerification(dynamicConfig.getString("verification"));sysInfo.setSystemRoot(dynamicConfig.getString("system.root"));return sysInfo;}

3.HostInformationController

package com.hero.lte.ems.sysmanager.resources;import com.hero.lte.ems.common.spring.SpringContextHolder;
import com.hero.lte.ems.sysconf.model.SysInfo;
import com.hero.lte.ems.sysmanager.entity.HostInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@RestController
@RequestMapping(value = "lte/ems/sysmanager/host")
@Api(value = "HostInformationController",tags={"系统信息"})
public class HostInformationController {private static Logger logger = LoggerFactory.getLogger(HostInformationController.class);public static String CENTOS_SYSTEM = "CentOS";public static String UBUNTU_SYSTEM = "Ubuntu";public static String REDOS_SYSTEM = "RED OS";@RequestMapping(value = "info", method = RequestMethod.GET)@ApiOperation("系统信息")public HostInfo getMailConfig() throws Exception{SysInfo sysInfo = SpringContextHolder.getBean(SysInfo.class);List<String> list = getAllHostIp();final StringBuilder ip = new StringBuilder("");if(list!=null&&list.size()>0){list.forEach(o->{ip.append(o+"/");});ip.deleteCharAt(ip.length()-1);}HostInfo hostInfo = new HostInfo();//hostInfo.setIp(sysInfo.getEmsCommIp());Map<String, String> map = getHostnameAndSystem();hostInfo.setIp(ip.toString());hostInfo.setVersion(sysInfo.getSystemVersion());hostInfo.setHostname(map.get("hostname").equals("n/a") ? "localhost" : map.get("hostname"));hostInfo.setOs(map.get("os"));return hostInfo;}public Map<String, String> getHostnameAndSystem() {Map<String, String> map = new HashMap<>();InputStream in = null;BufferedReader read = null;Process pro = null;String cmd = "hostnamectl | awk -F': +' '/Static hostname|Operating System/ {printf \"%s,\", $2}' | sed 's/,$//'";String[] cmds = null;try {String result = getSingleResult(cmd, cmds, pro, in, read);logger.info("result:{}", result);String[] split = result.split(",");map.put("hostname", split[0]);map.put("os", split[1]);} catch (IOException | InterruptedException e) {logger.error("-getHostnameAndSystem-Exception:{}", e);return null;} finally {try {if (pro != null) {pro.destroy();}if (read != null) {read.close();}if (in != null) {in.close();}} catch (IOException e) {logger.error("-getHostnameAndSystem-finally-IOException:{}", e);}}return map;}public static String getSingleResult(String cmd, String[] cmds, Process pro, InputStream in, BufferedReader read) throws IOException, InterruptedException {cmds = new String[]{"/bin/sh", "-c", cmd};logger.info("-cmd:{}", cmd);pro = Runtime.getRuntime().exec(cmds);if (pro.waitFor() == 0) {String line = "";in = pro.getInputStream();read = new BufferedReader(new InputStreamReader(in));while ((line = read.readLine()) != null) {logger.info("-line:{}", line);return line;}}return null;}/*** 获取该主机上所有网卡的ip*/public static ArrayList<String> getAllHostIp(){ArrayList<String> hostAddress = new ArrayList<>();try{String systemType = createSystemMonitor();// 获得本机的所有网络接口Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();while (nifs.hasMoreElements()) {NetworkInterface nif = nifs.nextElement();//查询Centos系统参数if(systemType.equals(CENTOS_SYSTEM) && nif.getName().startsWith("eth") || nif.getName().startsWith("eno")) {// 获得与该网络接口绑定的 IP 地址,一般只有一个Enumeration<InetAddress> addresses = nif.getInetAddresses();while (addresses.hasMoreElements()) {InetAddress addr = addresses.nextElement();if (addr instanceof Inet4Address) { // 只关心 IPv4 地址hostAddress.add(addr.getHostAddress());}}} else if(systemType.equals(UBUNTU_SYSTEM) && nif.getName().startsWith("enp")) {//查询ubuntu系统参数,获得与该网络接口绑定的 IP 地址,一般只有一个Enumeration<InetAddress> addresses = nif.getInetAddresses();while (addresses.hasMoreElements()) {InetAddress addr = addresses.nextElement();if (addr instanceof Inet4Address) { // 只关心 IPv4 地址hostAddress.add(addr.getHostAddress());}}} else if(systemType.equals(REDOS_SYSTEM) && nif.getName().startsWith("ens")) {logger.info("nif.getName():{}", nif.getName());//查询redos系统参数,获得与该网络接口绑定的 IP 地址,一般只有一个Enumeration<InetAddress> addresses = nif.getInetAddresses();while (addresses.hasMoreElements()) {InetAddress addr = addresses.nextElement();if (addr instanceof Inet4Address) {hostAddress.add(addr.getHostAddress());}}}}}catch(Exception e){e.printStackTrace();}return hostAddress;}public static String createSystemMonitor() {String systemType = "";InputStream in = null;BufferedReader read = null;Process pro = null;String cmd = "hostnamectl | awk -F': +' '/Static hostname|Operating System/ {printf \"%s,\", $2}' | sed 's/,$//'";String[] cmds = null;try {String result = getSingleResult(cmd, cmds, pro, in, read);String[] split = result.split(",");String os = split[1];if (os.contains(CENTOS_SYSTEM)) {systemType = CENTOS_SYSTEM;} else if (os.contains(UBUNTU_SYSTEM)) {systemType = UBUNTU_SYSTEM;} else if (os.contains(REDOS_SYSTEM)) {systemType = REDOS_SYSTEM;}} catch (IOException | InterruptedException e) {logger.error("-createSystemMonitor-Exception:{}", e);return null;} finally {try {if (pro != null) {pro.destroy();}if (read != null) {read.close();}if (in != null) {in.close();}} catch (IOException e) {logger.error("-createSystemMonitor-finally-IOException:{}", e);}}return systemType;}}

4.HostInfo

package com.hero.lte.ems.sysmanager.entity;public class HostInfo {private String os = "CentOS Linux release 7.7.1908 (Core)";;private String version;private String ip;private String hostname = "localhost.localdomain";public String getOs() {return os;}public void setOs(String os) {this.os = os;}public String getVersion() {return version;}public void setVersion(String version) {this.version = version;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public String getHostname() {return hostname;}public void setHostname(String hostname) {this.hostname = hostname;}@Overridepublic String toString() {return "HostInfo{" +"os='" + os + '\'' +", version='" + version + '\'' +", ip='" + ip + '\'' +", hostname='" + hostname + '\'' +'}';}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/79438.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/79438.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

阿里云自动备份网站,阿里云自动备份网站的方法

阿里云提供了多种自动备份网站的方法&#xff0c;适用于不同场景和需求&#xff0c;用户可根据自身技术能力和业务要求选择合适的方案。以下是几种主流的自动备份方法及操作要点&#xff1a; 一、基于云服务器ECS的自动快照备份 适用场景&#xff1a;适用于基于ECS部署的网站…

输入输出(python)

open&#xff08;&#xff09;需要和close&#xff08;&#xff09;配合使用 with open () as 不需要用close&#xff08;&#xff09;函数 在python3.0中的一些变动&#xff1a; eval 是编程语言中用于动态执行字符串形式代码的内置函数 &#xff0c;名称源于英文 “evaluate”…

Arduino逻辑控制详细解答,一点自己的想法记录

一、逻辑控制的基础概念与核心语法 1.1 逻辑控制的基本原理 逻辑控制是嵌入式系统中最常见的功能之一,其核心在于通过条件判断(if-else)、循环(for/while)和布尔运算(&&/||)实现对硬件的精确控制。例如,通过按键状态切换LED亮度、根据传感器数据调整电机转速…

字符串的相关方法

1. equals方法的作用 方法介绍 public boolean equals(String s) 比较两个字符串内容是否相同、区分大小写 示例代码 public class StringDemo02 {public static void main(String[] args) {//构造方法的方式得到对象char[] chs {a, b, c};String s1 new String(chs);…

JAVA基础:Collections 工具类实战指南-从排序到线程安全

在 Java 开发中&#xff0c;集合类几乎贯穿每一个项目&#xff0c;而Collections工具类提供了一系列强大的方法&#xff0c;用于操作和增强集合的功能。无论是排序、查找还是线程安全的封装&#xff0c;Collections工具类都是提升代码效率和质量的重要工具。 一、Collections …

ReLU函数及其Python实现

ReLU函数及其Python实现 文章目录 ReLU函数及其Python实现1. ReLU函数定义2. Python实现3. 在深度学习中的应用总结 1. ReLU函数定义 ReLU&#xff08;Rectified Linear Unit&#xff0c;修正线性单元&#xff09;函数是深度学习中常用的激活函数之一。它的定义非常简单&#…

2505ahk,wmi学习

检索每个服务的状态和启动类型 wbemServices : ComObjGet("winmgmts:\\.") //.代表本地计算机. wbemObjectSet : wbemServices.InstancesOf("Win32_Service")For wbemObject In wbemObjectSetMsgBox, % "Display Name: " wbemObject.DisplayNam…

大语言模型能力评定探讨

有标准答案的评估&#xff08;选择题&#xff09; 评估语言模型能力的基本思路是准备输入和标准答案&#xff0c;比较不同模型对相同输入的输出 由于AI答题有各种各样答案&#xff0c;因此现在是利用选择题考察。 有一个知名的选择题的基准叫做Massive Multitask Language Und…

数字智慧方案5874丨智慧交通收费稽核管理体系的构建与思考(44页PPT)(文末有下载方式)

资料解读&#xff1a;智慧交通收费稽核管理体系的构建与思考 详细资料请看本解读文章的最后内容。 随着高速公路收费系统的不断升级&#xff0c;特别是撤站后的新形势&#xff0c;收费稽核管理体系的构建显得尤为重要。本文将对辽宁省在联网收费新形势下的收费稽核管理体系进…

3.Java转义字符

Java转义字符 转义字符以\开头&#xff0c;常见的转义字符&#xff1a; 转义字符作用\t &#x1f31f;水平制表符&#xff08;Tab&#xff09;\r &#x1f31f;“回车&#xff08;Carriage Return&#xff09;”\n换行&#xff08;New Line&#xff09;\\输出一个反斜杠 \\&q…

【凑修电脑的小记录】vscode打不开

想把vscode的数据和环境从c盘移到d盘 大概操作和这篇里差不多 修改『Visual Studio Code&#xff08;VS Code&#xff09;』插件默认安装路径的方法 - 且行且思 - 博客园 在原地址保留了个指向新地址的链接文件。 重新安装vscode后双击 管理员身份运行均无法打开&#xff0…

MSP430G2553驱动0.96英寸OLED(硬件iic)

1.前言 最近需要用MSP430单片机做一个大作业,需要用到OLED模块&#xff0c;在这里记录一下 本篇文章主要讲解MSP430硬件iic的配置和OLED函数的调用&#xff0c;不会详细讲解OLED显示原理(其实就是江科大的OLED模块如何移植到msp430上).OLED显示原理以及底层函数讲解请参考其他…

SEO长尾词精准优化实战

内容概要 在搜索引擎优化领域&#xff0c;长尾关键词的精准挖掘与优化已成为突破流量瓶颈的核心策略。相较于通用词汇&#xff0c;长尾词具备更强的用户意图指向性与竞争分散特征&#xff0c;能够有效触达细分需求场景下的高价值受众。本部分将从长尾词的核心价值出发&#xf…

计算机组成原理实验(6) 微程序控制单元实验

实验六 微程序控制单元实验 一、实验目的 1、熟悉微程序控制器的原理 2、掌握微程序编制、写入并观察运行状态 二、实验要求 按照实验步骤完成实验项目&#xff0c;掌握设置微地址、微指令输出的方法 三、实验说明 3.1 微程序控制单元的构成&#xff1a;&#xff08;…

ECMAScript 2(ES2):标准化的微调与巩固

1. 版本背景与发布 发布时间&#xff1a;1998 年 6 月&#xff0c;由 ECMA International 正式发布&#xff0c;标准编号为 ECMA-262 Edition 2。核心定位&#xff1a;作为 ECMAScript 标准的第二次修订版&#xff0c;ES2 的核心目标是修正 ES1 中的错误、完善规范定义&#x…

基于蒙特卡洛模拟的电路容差分析与设计优化

蒙特卡洛模拟在电路设计中的应用 背景知识&#xff1a; 蒙特卡洛模拟是一种通过随机抽样来解决问题的数值方法。在电路设计中&#xff0c;它通过在元件参数的公差范围内随机生成大量样本值&#xff0c;模拟电路在不同参数组合下的行为&#xff0c;从而评估和优化电路设计&…

node.js 实战——mongoDB

MongoDB MongoDB 简介 MongoDB 是一种基于文档型 (document-oriented) 的 NoSQL 数据库&#xff0c;使用类 JSON 的 BSON 格式存储数据&#xff0c;自然支持复杂数据结构。它特别适合需要快速变化、大量数据处理和高应用扩展性的场景。 MongoDB 特性&#xff1a; 无法表、无…

如何掌握 Lustre/Scade 同步数据流语言

从 KPN 的萌芽开始&#xff0c;到 Lustre/Scade 的发展&#xff0c;再到 Velus/Zelus/Swan 在形式化编译、连续时间建模、MBD 平权等各方面的边界拓展&#xff0c;同步数据流语言已经历许多。现在&#xff0c;我们讨论如何掌握 Lustre/Scade 这类法式技术&#xff0c;从语言基础…

神州趣味地名-基于天地图和LeafLet的趣味地名探索

目录 前言 一、搜索API据介绍 1、官方API 2、Leaflet集成 二、成果介绍 1、令人忍俊不禁的地名 2、黑地名 3、数字地名 4、文艺地名 三、总结 前言 在华夏大地广袤的土地上&#xff0c;地名承载着深厚的历史文化底蕴&#xff0c;它们如同一颗颗璀璨的明珠&#xff0c;…

第6篇:EggJS数据库操作与ORM实践

在Web应用开发中&#xff0c;数据库操作是核心环节之一。EggJS通过集成Sequelize ORM框架&#xff0c;提供了高效、安全的数据库操作方案。本文将深入讲解如何在EggJS中配置MySQL数据库、定义数据模型、优化复杂查询&#xff0c;以及管理数据库迁移与种子数据。 一、MySQL基础配…