Golang獲取MacOSX網(wǎng)卡信息

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

1. net包獲取

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

// 2. 根據(jù)網(wǎng)卡Index或網(wǎng)卡Name獲取
addr, err := net.InterfaceByIndex(index) // 根據(jù)index獲取
addr, err := net.InterfaceByName(name) // 根據(jù)name獲取

// 3. net包可以獲取到的網(wǎng)卡信息
// 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子網(wǎng)掩碼 ifconfig | grep netmask
exec.Command("bash", "-c", "ifconfig | grep netmask")
// 3. 獲取默認(rèn)網(wǎng)關(guān)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. 獲取網(wǎng)卡類型  networksetup -listallhardwareports
exec.Command("networksetup", "-listallhardwareports")
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容