Go语言获取Ubuntu所有网卡名


Go语言获取Ubuntu所有网卡名

需求

  • 获取当前机器下所有网卡名,以字符串数组的形式返回

实现demo

package main

import (
	"fmt"
	"os/exec"
	"strings"
)

func main() {
	s := GetLocalNetDeviceNames()
	fmt.Println(s)
}

func GetLocalNetDeviceNames() []string {
	baseNicPath := "/sys/class/net/"
	cmd := exec.Command("ls", baseNicPath)
	buf, _ := cmd.Output()
	output := string(buf)
	str := ""
	for _, device := range strings.Split(output, "\n") {
		if len(device) > 1 {
			if device != "lo" {
				str += device+"|"
			}
		}
	}
	return strings.Split(str[:len(str)-1],"|")
}

输出

mv@mv-Super-Server:~$ ./test
[eno1 eno2]


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM