Certainly! Below is a Go application that scrapes Windows WiFi quality metrics without using the netsh command. This example uses the wlanapi to interact with the WiFi interface directly.
First, you need to install the necessary Go packages. You can use go get to install them:
go get golang.org/x/sys/windows
go get golang.org/x/text/encoding/unicode
Here's the Go application:
package main
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
const (
DOT11_BSS_TYPE_INFRASTRUCTURE = 1
DOT11_BSS_TYPE_INDEPENDENT = 2
DOT11_BSS_TYPE_ANY = 3
)
var (
wlanapi = syscall.MustLoadDLL("wlanapi.dll")
WlanOpenHandle = wlanapi.MustFindProc("WlanOpenHandle")
WlanEnumInterfaces = wlanapi.MustFindProc("WlanEnumInterfaces")
WlanGetNetworkBssList = wlanapi.MustFindProc("WlanGetNetworkBssList")
WlanFreeMemory = wlanapi.MustFindProc("WlanFreeMemory")
)
type WLAN_INTERFACE_INFO struct {
InterfaceGuid windows.GUID
strInterfaceDescription [256]uint16
}
type WLAN_BSS_ENTRY struct {
dot11BssType uint32
strProfileName [256]uint16
dot11Bssid [6]byte
strSSID [32]uint16
dot11PhyType uint32
dot11BssPhyType uint32
dot11AuthenticationAlgorithm uint32
dot11CipherAlgorithm uint32
dot11RsnCipherAlgorithm uint32
lRssi int32
dot11BssidStr [32]uint16
ullTimestamp uint64
}
type WLAN_INTERFACE_INFO_LIST struct {
dwNumberOfItems uint32
dwIndex uint32
InterfaceInfo [1]WLAN_INTERFACE_INFO
}
type WLAN_BSS_LIST struct {
wlanBssCount uint32
wlanBssEntries [1]WLAN_BSS_ENTRY
}
func main() {
var hClient windows.Handle
var dwNegCaps uint32
ver := [2]uint32{1, 0}
ret, _, _ := WlanOpenHandle.Call(uintptr(unsafe.Pointer(&ver)), 0, uintptr(unsafe.Pointer(&dwNegCaps)), uintptr(unsafe.Pointer(&hClient)))
if ret != 0 {
fmt.Printf("WlanOpenHandle failed with error: %d\n", ret)
return
}
defer WlanFreeMemory.Call(uintptr(hClient))
var pIfList uintptr
ret, _, _ = WlanEnumInterfaces.Call(uintptr(hClient), 0, uintptr(unsafe.Pointer(&pIfList)))
if ret != 0 {
fmt.Printf("WlanEnumInterfaces failed with error: %d\n", ret)
return
}
defer WlanFreeMemory.Call(pIfList)
ifList := (*WLAN_INTERFACE_INFO_LIST)(unsafe.Pointer(pIfList))
if ifList.dwNumberOfItems == 0 {
fmt.Println("No WiFi interfaces found")
return
}
for i := uint32(0); i < ifList.dwNumberOfItems; i++ {
interfaceInfo := &ifList.InterfaceInfo[i]
fmt.Printf("Interface %d: %s\n", i, syscall.UTF16ToString(interfaceInfo.strInterfaceDescription[:]))
var pBssList uintptr
ret, _, _ = WlanGetNetworkBssList.Call(uintptr(hClient), interfaceInfo.InterfaceGuid.Ptr(), DOT11_BSS_TYPE_ANY, 0, 0, uintptr(unsafe.Pointer(&pBssList)), 0)
if ret != 0 {
fmt.Printf("WlanGetNetworkBssList failed with error: %d\n", ret)
continue
}
defer WlanFreeMemory.Call(pBssList)
bssList := (*WLAN_BSS_LIST)(unsafe.Pointer(pBssList))
for j := uint32(0); j < bssList.wlanBssCount; j++ {
bssEntry := &bssList.wlanBssEntries[j]
fmt.Printf(" SSID: %s, RSSI: %d\n", syscall.UTF16ToString(bssEntry.strSSID[:]), bssEntry.lRssi)
}
}
}
Explanation:
- DLL Imports: The application imports the necessary functions from
wlanapi.dll.
- Structures: Defines the structures required to interact with the WiFi API.
- WlanOpenHandle: Opens a handle to the WiFi service.
- WlanEnumInterfaces: Enumerates the available WiFi interfaces.
- WlanGetNetworkBssList: Retrieves the list of BSS entries (access points) for a given interface.
- Memory Management: Frees the memory allocated by the WiFi API functions.
Running the Application:
- Ensure you have the necessary permissions to access WiFi information.
- Compile and run the Go application:
go run main.go
This will print the SSIDs and RSSI values of the available WiFi networks.