-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (39 loc) · 922 Bytes
/
main.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
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"net"
)
func main() {
ipAndMask := getIPAndMask()
for interfaceName, info := range ipAndMask {
fmt.Printf("[%s] %s/%s\n", interfaceName, info.ip, info.netmask)
}
}
type IPInfo struct {
ip string
netmask string
}
func getIPAndMask() map[string]IPInfo {
ipMaskList := make(map[string]IPInfo)
interfaces, err := net.Interfaces()
if err != nil {
fmt.Println("Error getting network interfaces:", err)
return ipMaskList
}
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
fmt.Println("Error getting addresses for interface", iface.Name, ":", err)
continue
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && ipNet.IP.To4() != nil { // Check for IPv4
ipMaskList[iface.Name] = IPInfo{
ip: ipNet.IP.String(),
netmask: net.IP(ipNet.Mask).String(),
}
}
}
}
return ipMaskList
}