Golang : How to detect a server/machine network interface capabilities?
Problem :
You want to know which network interfaces on a server that support Broadcast, Loopback, PointToPoint or Multicast networking capabilities. How to do that in Golang?
Solution :
Use the net.Interfaces()
function to retrieve all the available network interfaces on the server, then check each interfaces' Flags.
For example :
package main
import (
"fmt"
"net"
"strings"
)
func main() {
interfaces, err := net.Interfaces()
if err != nil {
fmt.Print(err)
return
}
for _, i := range interfaces {
fmt.Printf("Name : %v \n", i.Name)
// see http://golang.org/pkg/net/#Flags
fmt.Println("Interface type and supports : ", i.Flags.String())
if strings.Contains(i.Flags.String(), "up") {
fmt.Println("Status : UP")
} else {
fmt.Println("Status : DOWN")
}
if strings.Contains(i.Flags.String(), "multicast") {
fmt.Println("Support multicast : YES")
} else {
fmt.Println("Support multicast : NO")
}
}
}
Sample output :
Name : lo0
Interface type and supports : up|loopback|multicast
Status : UP
Support multicast : YES
Name : gif0
Interface type and supports : pointtopoint|multicast
Status : DOWN
Support multicast : YES
Name : stf0
Interface type and supports : 0
Status : DOWN
Support multicast : NO
Name : en1
Interface type and supports : up|broadcast|multicast
Status : UP
Support multicast : YES
References :
http://golang.org/pkg/net/#Interfaces
http://golang.org/pkg/net/#Flags
const (
FlagUp Flags = 1 << iota // interface is up
FlagBroadcast // interface supports broadcast access capability
FlagLoopback // interface is a loopback interface
FlagPointToPoint // interface belongs to a point-to-point link
FlagMulticast // interface supports multicast access capability
)
https://www.socketloop.com/references/golang-net-flags-type-and-flags-string-function-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.6k Golang : Get UDP client IP address and differentiate clients by port number
+13.6k Golang : How to check if a file is hidden?
+5.6k Unix/Linux : How to test user agents blocked successfully ?
+16.2k Golang : Send email and SMTP configuration example
+33.7k Golang : Call a function after some delay(time.Sleep and Tick)
+9.5k PHP : Get coordinates latitude/longitude from string
+10.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+8k Golang : Emulate NumPy way of creating matrix example
+8.6k Golang : Heap sort example
+17.3k Golang : Find smallest number in array
+14.3k Golang : Overwrite previous output with count down timer
+6.1k Unix/Linux : Use netstat to find out IP addresses served by your website server