Golang获取MacOSX网卡信息

由于macos并没有提供更多接口可以使用,很多信息只能用cmd命令解析

1. net包获取

// 1. 批量获取
addrs, err := net.Interfaces()

// 2. 根据网卡Index或网卡Name获取
addr, err := net.InterfaceByIndex(index) // 根据index获取
addr, err := net.InterfaceByName(name) // 根据name获取

// 3. net包可以获取到的网卡信息
// Interface represents a mapping between network interface name
// and index. It also represents network interface facility
// information.
type Interface struct {
    Index        int          // positive integer that starts at one, zero is never used
    MTU          int          // maximum transmission unit
    Name         string       // e.g., "en0", "lo0", "eth0.100"
    HardwareAddr HardwareAddr // IEEE MAC-48, EUI-48 and EUI-64 form
    Flags        Flags        // e.g., FlagUp, FlagLoopback, FlagMulticast
}

2. cmd获取

// 1. 获取DHCP ipconfig getpacket eth_name
exec.Command("ipconfig", "getpacket", eth)
// 2. 获取IP子网掩码 ifconfig | grep netmask
exec.Command("bash", "-c", "ifconfig | grep netmask")
// 3. 获取默认网关gateway  route get default
exec.Command("route", "get", "default")
// 4. 获取wifi ssid  /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -
exec.Command("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport", "-I")
// 5. 获取网卡类型  networksetup -listallhardwareports
exec.Command("networksetup", "-listallhardwareports")
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容