-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ip.go
37 lines (32 loc) · 927 Bytes
/
get_ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"fmt"
"net"
)
func getWLANIPAddress() (net.IP, error) {
// Get all network interfaces
interfaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("error getting network interfaces: %v", err)
}
// Find the IP address of the first WLAN interface
for _, iface := range interfaces {
if iface.Flags&net.FlagUp != 0 && iface.Flags&net.FlagLoopback == 0 {
if iface.Name == "Wi-Fi" {
// Get interface addresses
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("error getting addresses for interface %s: %v", iface.Name, err)
}
// Find the first non-loopback IP address
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && ipNet.IP.To4() != nil && !ipNet.IP.IsLoopback() {
return ipNet.IP, nil
}
}
}
}
}
return nil, fmt.Errorf("no WLAN interface found or no IP address associated")
}