你可以在GO语言程序中如何检测Linux发行版?

huangapple go评论74阅读模式
英文:

How can I detect linux distribution within GOlang program?

问题

我想编写与Linux发行版无关的Golang代码。我需要检测当前的Linux发行版,并在程序中运行特定于该发行版的命令。例如,在Ubuntu中使用dpkg,在RHEL中使用rpm -q。

英文:

I want to write linux distribution independant Golang code. I need detect which linux distribution and need to run distribution specific commands within program. Like dpkg in case of Ubuntu and rpm -q in case of RHEL.

答案1

得分: 4

你可以使用exec.Cmd来运行lsb_release -auname -a命令,并解析输出结果以查找发行版信息。

参考链接

英文:

You can use exec.Cmd to run lsb_release -a or uname -a and parse the output to find out the distribution.

Reference

答案2

得分: 3

如果你想使用一个Go模块,可以使用Sysinfo

它会收集系统的所有信息(不依赖其他库),并以JSON格式返回包含你所需信息的数据。以下是其中一部分数据的示例:

"os": {
    "name": "CentOS Linux 7 (Core)",
    "vendor": "centos",
    "version": "7",
    "release": "7.2.1511",
    "architecture": "amd64"
  }
英文:

If you wanted to use a Go module, there's Sysinfo.

It collects all of the system info (without any dependencies) and gives you JSON which includes what you're looking for. Here's a snippet of that data:

"os": {
    "name": "CentOS Linux 7 (Core)",
    "vendor": "centos",
    "version": "7",
    "release": "7.2.1511",
    "architecture": "amd64"
  },

答案3

得分: 2

包含可执行文件 lsb_release 的软件包可能已经安装在系统上,也可能没有安装。

您可以使用文件 /etc/os-release,该文件可以使用 go-ini 库轻松解析。以下是示例代码:

import (
    "fmt"
    "github.com/go-ini/ini"
)

func ReadOSRelease(configfile string) map[string]string {
    cfg, err := ini.Load(configfile)
    if err != nil {
        log.Fatal("Fail to read file: ", err)
    }

    ConfigParams := make(map[string]string)
    ConfigParams["ID"] = cfg.Section("").Key("ID").String()

    return ConfigParams
}

OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"]

fmt.Print(OSRelease)
英文:

The package containing the executable lsb_release may or may not be installed on the system.

You could use the file /etc/os-release, which can be easily parsed with the library go-ini. See example:

import (
    "fmt"
	"github.com/go-ini/ini"
)

func ReadOSRelease(configfile string) map[string]string {
	cfg, err := ini.Load(configfile)
	if err != nil {
		log.Fatal("Fail to read file: ", err)
	}

	ConfigParams := make(map[string]string)
	ConfigParams["ID"] = cfg.Section("").Key("ID").String()

	return ConfigParams
}

OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"]

fmt.Print(OSRelease)

答案4

得分: 1

这里有一个方便的命令行工具,由一个Go库支持:dekobon/distro-detect

这是命令行主函数中调用该库的方式

linux.FileSystemRoot = fsRoot
distro := linux.DiscoverDistro()

它解析了os-release文件和lsb-release文件的声明。

英文:

There's a convenient command line tool backed by a go library here: dekobon/distro-detect

Here's how the library is called in the main function of the command line

linux.FileSystemRoot = fsRoot
distro := linux.DiscoverDistro()

It parses both the os-release file and the lsb-release file declarations.

huangapple
  • 本文由 发表于 2017年2月22日 16:56:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/42386788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定