由于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")