Here's a Go application that scrapes WiFi quality metrics on Windows without using netsh. It uses the Windows Native Wifi API through Go's syscall package to access WiFi information directly.
go
package main
import (
"encoding/binary"
"fmt"
"syscall"
"time"
"unsafe"
)
// Windows API constants and types
const (
wlan_interface_state_connected = 1
wlan_intf_opcode_current_connection = 0x00000001
)
type WLAN_INTERFACE_INFO struct {
InterfaceGuid syscall.GUID
strInterfaceDescription [256]uint16
isState uint32
}
type WLAN_INTERFACE_INFO_LIST struct {
dwNumberOfItems uint32
dwIndex uint32
InterfaceInfo [1]WLAN_INTERFACE_INFO
}
type WLAN_CONNECTION_ATTRIBUTES struct {
isState uint32
wlanConnectionMode uint32
strProfileName [256]uint16
wlanAssociationAttributes WLAN_ASSOCIATION_ATTRIBUTES
wlanSecurityAttributes WLAN_SECURITY_ATTRIBUTES
}
type WLAN_ASSOCIATION_ATTRIBUTES struct {
dot11Ssid DOT11_SSID
dot11BssType uint32
dot11MacAddress [6]byte
dot11PhyType uint32
ulDot11PhyIndex uint32
wlanSignalQuality uint32
ulRxRate uint32
ulTxRate uint32
}
type DOT11_SSID struct {
uSSIDLength uint32
ucSSID [32]byte
}
type WLAN_SECURITY_ATTRIBUTES struct {
bSecurityEnabled uint32
bOneXEnabled uint32
dot11AuthAlgorithm uint32
dot11CipherAlgorithm uint32
}
var (
wlanapi = syscall.NewLazyDLL("wlanapi.dll")
procWlanOpenHandle = wlanapi.NewProc("WlanOpenHandle")
procWlanEnumInterfaces = wlanapi.NewProc("WlanEnumInterfaces")
procWlanQueryInterface = wlanapi.NewProc("WlanQueryInterface")
procWlanCloseHandle = wlanapi.NewProc("WlanCloseHandle")
procWlanFreeMemory = wlanapi.NewProc("WlanFreeMemory")
procWlanGetAvailableNetworkList = wlanapi.NewProc("WlanGetAvailableNetworkList")
)
func main() {
// Open WLAN API handle
var negotiatedVersion uint32
var clientHandle syscall.Handle
ret, _, _ := procWlanOpenHandle.Call(
2, // Requested version 2.0
0,
uintptr(unsafe.Pointer(&negotiatedVersion)),
uintptr(unsafe.Pointer(&clientHandle)),
)
if ret != 0 {
fmt.Printf("WlanOpenHandle failed: %d\n", ret)
return
}
defer procWlanCloseHandle.Call(uintptr(clientHandle), 0)
// Enumerate interfaces
var interfaceList *WLAN_INTERFACE_INFO_LIST
ret, _, _ = procWlanEnumInterfaces.Call(
uintptr(clientHandle),
0,
uintptr(unsafe.Pointer(&interfaceList)),
)
if ret != 0 {
fmt.Printf("WlanEnumInterfaces failed: %d\n", ret)
return
}
defer procWlanFreeMemory.Call(uintptr(unsafe.Pointer(interfaceList)))
// Iterate through interfaces
numInterfaces := int(interfaceList.dwNumberOfItems)
if numInterfaces == 0 {
fmt.Println("No WiFi interfaces found")
return
}
// Calculate the offset to the array of WLAN_INTERFACE_INFO structs
base := uintptr(unsafe.Pointer(&interfaceList.InterfaceInfo[0]))
for i := 0; i < numInterfaces; i++ {
offset := uintptr(i) * unsafe.Sizeof(interfaceList.InterfaceInfo[0])
iface := (*WLAN_INTERFACE_INFO)(unsafe.Pointer(base + offset))
// Only show connected interfaces
if iface.isState == wlan_interface_state_connected {
printInterfaceInfo(clientHandle, iface)
}
}
}
func printInterfaceInfo(clientHandle syscall.Handle, iface *WLAN_INTERFACE_INFO) {
var dataSize uint32
var connectionAttributes *WLAN_CONNECTION_ATTRIBUTES
var wlanOpcodeValueType uint32
ret, _, _ := procWlanQueryInterface.Call(
uintptr(clientHandle),
uintptr(unsafe.Pointer(&iface.InterfaceGuid)),
wlan_intf_opcode_current_connection,
0,
uintptr(unsafe.Pointer(&dataSize)),
uintptr(unsafe.Pointer(&connectionAttributes)),
uintptr(unsafe.Pointer(&wlanOpcodeValueType)),
)
if ret != 0 {
fmt.Printf("WlanQueryInterface failed: %d\n", ret)
return
}
defer procWlanFreeMemory.Call(uintptr(unsafe.Pointer(connectionAttributes)))
// Print WiFi quality metrics
assoc := connectionAttributes.wlanAssociationAttributes
ssid := assoc.dot11Ssid.ucSSID[:assoc.dot11Ssid.uSSIDLength]
fmt.Printf("\n=== WiFi Quality Metrics ===\n")
fmt.Printf("Timestamp: %s\n", time.Now().Format(time.RFC3339))
fmt.Printf("SSID: %s\n", string(ssid))
fmt.Printf("Signal Quality: %d%%\n", assoc.wlanSignalQuality)
fmt.Printf("MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
assoc.dot11MacAddress[0], assoc.dot11MacAddress[1], assoc.dot11MacAddress[2],
assoc.dot11MacAddress[3], assoc.dot11MacAddress[4], assoc.dot11MacAddress[5])
fmt.Printf("PHY Type: %s\n", getPhyTypeName(assoc.dot11PhyType))
fmt.Printf("Receive Rate: %d Kbps\n", assoc.ulRxRate/1000)
fmt.Printf("Transmit Rate: %d Kbps\n", assoc.ulTxRate/1000)
}
func getPhyTypeName(phyType uint32) string {
switch phyType {
case 1:
return "802.11a"
case 2:
return "802.11b"
case 4:
return "802.11g"
case 8:
return "802.11n"
case 0x10:
return "802.11ac"
case 0x20:
return "802.11ax"
default:
return fmt.Sprintf("Unknown (%d)", phyType)
}
}
How This Works
WLAN API Initialization: The code starts by opening a handle to the Windows WLAN API.
Interface Enumeration: It enumerates all available WiFi interfaces on the system.
Connection Query: For each connected interface, it queries the current connection attributes.
Metrics Extraction: From the connection attributes, it extracts:
SSID (network name)
Signal quality (0-100%)
MAC address of the access point
PHY type (802.11 standard)
Receive/transmit rates
Display: The metrics are printed to the console with a timestamp.
Building and Running
Save this code to a file named wifi_metrics.go
Build it with: go build wifi_metrics.go
Run the executable: wifi_metrics.exe
Notes
This requires Windows and won't work on other operating systems.
The application needs to be run with sufficient privileges (normal user should be fine).
The signal quality is reported as a percentage (0-100) where higher is better.
The code handles memory management properly with defer statements to free Windows API resources.
Would you like me to add any additional features or modify anything about this implementation?