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

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

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格式返回包含你所需信息的数据。以下是其中一部分数据的示例:

  1. "os": {
  2. "name": "CentOS Linux 7 (Core)",
  3. "vendor": "centos",
  4. "version": "7",
  5. "release": "7.2.1511",
  6. "architecture": "amd64"
  7. }
英文:

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:

  1. "os": {
  2. "name": "CentOS Linux 7 (Core)",
  3. "vendor": "centos",
  4. "version": "7",
  5. "release": "7.2.1511",
  6. "architecture": "amd64"
  7. },

答案3

得分: 2

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

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

  1. import (
  2. "fmt"
  3. "github.com/go-ini/ini"
  4. )
  5. func ReadOSRelease(configfile string) map[string]string {
  6. cfg, err := ini.Load(configfile)
  7. if err != nil {
  8. log.Fatal("Fail to read file: ", err)
  9. }
  10. ConfigParams := make(map[string]string)
  11. ConfigParams["ID"] = cfg.Section("").Key("ID").String()
  12. return ConfigParams
  13. }
  14. OSInfo := ReadOSRelease("/etc/os-release")
  15. OSRelease := OSInfo["ID"]
  16. 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:

  1. import (
  2. "fmt"
  3. "github.com/go-ini/ini"
  4. )
  5. func ReadOSRelease(configfile string) map[string]string {
  6. cfg, err := ini.Load(configfile)
  7. if err != nil {
  8. log.Fatal("Fail to read file: ", err)
  9. }
  10. ConfigParams := make(map[string]string)
  11. ConfigParams["ID"] = cfg.Section("").Key("ID").String()
  12. return ConfigParams
  13. }
  14. OSInfo := ReadOSRelease("/etc/os-release")
  15. OSRelease := OSInfo["ID"]
  16. fmt.Print(OSRelease)

答案4

得分: 1

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

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

  1. linux.FileSystemRoot = fsRoot
  2. 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

  1. linux.FileSystemRoot = fsRoot
  2. 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:

确定