File: example_link_sethwaddr_test.go

package info (click to toggle)
golang-github-jsimonetti-rtnetlink 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,264 kB
  • sloc: makefile: 2
file content (43 lines) | stat: -rw-r--r-- 872 bytes parent folder | download | duplicates (2)
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
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)
}