功能:获取windows平台下、获取本机的物理网卡、排除虚拟网卡和USB外置网卡
实现
package main
// 获取本机pci接口的网卡信息、去除了虚拟网卡和usb网卡等信息
// 可获取到的网卡信息:网卡ID、网卡IPV4地址、网卡IPV6地址、MAC地址
import (
"fmt"
"github.com/StackExchange/wmi"
"golang.org/x/sys/windows/registry"
"log"
"strings"
"time"
)
type Win32_NetworkAdapterConfiguration struct {
Caption string
Description string
SettingID string
ArpAlwaysSourceRoute bool
ArpUseEtherSNAP bool
DatabasePath string
DeadGWDetectEnabled bool
DefaultIPGateway []string
DefaultTOS uint8
DefaultTTL uint8
DHCPEnabled bool
DHCPLeaseExpires *time.Time
DHCPLeaseObtained *time.Time
DHCPServer string
DNSDomain string
DNSDomainSuffixSearchOrder []string
DNSEnabledForWINSResolution bool
DNSHostName string
DNSServerSearchOrder []string
DomainDNSRegistrationEnabled bool
ForwardBufferMemory uint32
FullDNSRegistrationEnabled bool
GatewayCostMetric []int32
IGMPLevel uint8
Index uint32
InterfaceIndex uint32
IPAddress []string
IPConnectionMetric uint32
IPEnabled bool
IPFilterSecurityEnabled bool
IPPortSecurityEnabled bool
IPSecPermitIPProtocols []string
IPSecPermitTCPPorts []string
IPSecPermitUDPPorts []string
IPSubnet []string
IPUseZeroBroadcast bool
IPXAddress string
IPXEnabled bool
IPXFrameType []uint32
IPXMediaType uint32
IPXNetworkNumber []string
IPXVirtualNetNumber string
KeepAliveInterval uint32
KeepAliveTime uint32
MACAddress string
MTU uint32
NumForwardPackets uint32
PMTUBHDetectEnabled bool
PMTUDiscoveryEnabled bool
ServiceName string
TcpipNetbiosOptions uint32
TcpMaxConnectRetransmissions uint32
TcpMaxDataRetransmissions uint32
TcpNumConnections uint32
TcpUseRFC1122UrgentPointer bool
TcpWindowSize uint16
WINSEnableLMHostsLookup bool
WINSHostLookupFile string
WINSPrimaryServer string
WINSScopeID string
WINSSecondaryServer string
}
type NetWorkInterface struct {
Type uint64
Name string
PnPInstanceId string
IP4Addr string
IP6Addr string
MACAddr string
}
// 通过wmi接口获取ip网卡信息
func WbemQuery() []NetWorkInterface {
s, err := wmi.InitializeSWbemServices(wmi.DefaultClient)
if err != nil {
log.Fatalf("InitializeSWbemServices: %s", err)
}
var dst []Win32_NetworkAdapterConfiguration
q := wmi.CreateQuery(&dst, "WHERE IPEnabled=True")
errQuery := s.Query(q, &dst)
if errQuery != nil {
log.Fatalf("Query1: %s", errQuery)
}
var netInterface []NetWorkInterface
for _, value := range dst {
netInfo, flag := ReadRegedit(value.SettingID)
if flag {
result := NetWorkInterface{
Type: netInfo.Type,
Name: netInfo.Name,
PnPInstanceId: netInfo.PnPInstanceId,
IP4Addr: value.IPAddress[0],
IP6Addr: value.IPAddress[1],
MACAddr: value.MACAddress,
}
netInterface = append(netInterface, result)
}
}
return netInterface
}
// 读取注册表、获取网卡类型、判断PCI标识
func ReadRegedit(netId string) (NetWorkInterface, bool) {
var result NetWorkInterface
key, err := registry.OpenKey(registry.LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"+netId+"\\Connection", registry.READ)
if err != nil {
log.Fatal(err)
}
defer key.Close()
value, _, err := key.GetStringValue("PnPInstanceId")
Type, _, err := key.GetIntegerValue("MediaSubType")
Name, _, err := key.GetStringValue("Name")
if err != nil {
return result, false
}
if strings.Count(value, "PCI") > 0 {
result.Type = Type
result.Name = Name
result.PnPInstanceId = value
return result, true
}
return result, false
}
func main() {
fmt.Println(WbemQuery())
}
网友评论