package main
import (
"encoding/json"
"fmt"
"github.com/StackExchange/wmi"
"log"
"time"
)
func main() {
WbemQuery()
}
func WbemQuery() {
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)
}
if r, e := json.Marshal(dst); e == nil {
fmt.Println(string(r))
} else {
fmt.Println(e)
}
}
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
}
结果如下:
[
{
"Caption": "[00000001] VMware Virtual Ethernet Adapter for VMnet1",
"Description": "VMware Virtual Ethernet Adapter for VMnet1",
"SettingID": "{A006AD5B-6613-4C5C-86A5-3BA1BCA6A34D}",
"ArpAlwaysSourceRoute": false,
"ArpUseEtherSNAP": false,
"DatabasePath": "%SystemRoot%\\System32\\drivers\\etc",
"DeadGWDetectEnabled": false,
"DefaultIPGateway": null,
"DefaultTOS": 0,
"DefaultTTL": 0,
"DHCPEnabled": true,
"DHCPLeaseExpires": "2020-04-22T09:28:02+08:00",
"DHCPLeaseObtained": "2020-04-22T08:58:02+08:00",
"DHCPServer": "192.168.106.254",
"DNSDomain": "",
"DNSDomainSuffixSearchOrder": [],
"DNSEnabledForWINSResolution": false,
"DNSHostName": "DESKTOP-28OR30T",
"DNSServerSearchOrder": null,
"DomainDNSRegistrationEnabled": false,
"ForwardBufferMemory": 0,
"FullDNSRegistrationEnabled": true,
"GatewayCostMetric":</