rtnetlink/example_link_sethwaddr_test.go
Ben Kochie 8026e5db33
Fix Go mod path (#226)
In order to update to v2.0.0, we need to update the module path per the
Go module documentation.
* https://go.dev/doc/modules/major-version

Fixes: https://github.com/jsimonetti/rtnetlink/issues/225

This can be released as v2.0.1

Signed-off-by: SuperQ <superq@gmail.com>
2024-05-10 16:40:56 +02:00

44 lines
872 B
Go

package rtnetlink_test
import (
"log"
"net"
"github.com/jsimonetti/rtnetlink/v2"
)
// Set the hw address of an interface
func Example_setLinkHWAddr() {
// Gather the interface Index
iface, _ := net.InterfaceByName("dummy0")
// Get a hw addr to set the interface to
hwAddr, _ := net.ParseMAC("ce:9c:5b:98:55:9c")
// Dial a connection to the rtnetlink socket
conn, err := rtnetlink.Dial(nil)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// Request the details of the interface
msg, err := conn.Link.Get(uint32(iface.Index))
if err != nil {
log.Fatal(err)
}
// Set the hw address of the interfaces
err = conn.Link.Set(&rtnetlink.LinkMessage{
Family: 0x0,
Type: msg.Type,
Index: uint32(iface.Index),
Flags: msg.Flags,
Change: msg.Change,
Attributes: &rtnetlink.LinkAttributes{
Address: hwAddr,
},
})
log.Fatal(err)
}