Skip to content

Commit 34efd52

Browse files
authored
Add the possibility to choose mac address with -mac (#5)
1 parent 4ae51e8 commit 34efd52

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
IP network from where the command will be executed (default "192.168.1.11/24")
1919
-log-level string
2020
min level of logs to print (default "info")
21+
-mac string
22+
mac address of the interface inside the namespace (default will be a random one)
2123
-ns-path string
2224
path of the temporary namespace to be created (default "/var/run/netns/w000t$PID")
2325

main.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/vishvananda/netns"
1818
)
1919

20-
var ip, command, gateway, intf, logLevel, nsPath string
20+
var ip, command, gateway, intf, logLevel, nsPath, mac string
2121
var log = logrus.New()
2222

2323
func init() {
@@ -26,6 +26,7 @@ func init() {
2626
flag.StringVar(&command, "command", "ip route", "command to be executed")
2727
flag.StringVar(&gateway, "gw", "", "gateway of the request (default will be the default route of the given interface)")
2828
flag.StringVar(&logLevel, "log-level", "info", "min level of logs to print")
29+
flag.StringVar(&mac, "mac", "", "mac address of the interface inside the namespace (default will be a random one)")
2930
flag.StringVar(
3031
&nsPath,
3132
"ns-path",
@@ -120,19 +121,28 @@ func main() {
120121
return
121122
}
122123

123-
err = netlink.LinkSetDown(macVlan)
124-
if err != nil {
125-
log.Warn("Error while setting macVlan down: ", err)
126-
return
127-
}
128-
129124
link, err := netlink.LinkByName("peth0")
130125
if err != nil {
131126
log.Warn("Error while getting macVlan: ", err)
132127
return
133128
}
134129
log.Debugf("MacVlan created : %+v", link)
135130

131+
// If a mac was specified, set it now
132+
if mac != "" {
133+
log.Debugf("Setting macVlan with specified MAC : %s", mac)
134+
hardwareAddr, err := net.ParseMAC(mac)
135+
if err != nil {
136+
log.Warn("Error while parsing given mac: ", err)
137+
return
138+
}
139+
err = netlink.LinkSetHardwareAddr(link, hardwareAddr)
140+
if err != nil {
141+
log.Warn("Error while setting given mac on macVlan: ", err)
142+
return
143+
}
144+
}
145+
136146
// ============================== Create the new Namespace
137147

138148
newns, err := newNS()

0 commit comments

Comments
 (0)