-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
ifaces.go
37 lines (33 loc) · 866 Bytes
/
ifaces.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 ifaces() {
// Get the list of network interfaces
interfaces, err := net.Interfaces()
if err != nil {
fmt.Println("Failed to retrieve network interfaces:", err)
return
}
// Iterate through the interfaces
for _, iface := range interfaces {
// Filter out loopback and non-up interfaces
if iface.Flags&net.FlagLoopback == 0 && iface.Flags&net.FlagUp != 0 {
addrs, err := iface.Addrs()
if err != nil {
fmt.Println("Failed to retrieve addresses for interface", iface.Name, ":", err)
continue
}
// Iterate through the addresses
for _, addr := range addrs {
// Check if the address is an IP address
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
fmt.Println("Local IP address:", ipnet.IP.String())
}
}
}
}
}
}