Skip to content

Commit

Permalink
Add the possibility to choose mac address with -mac (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
PouuleT authored Jul 26, 2016
1 parent 4ae51e8 commit 34efd52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
IP network from where the command will be executed (default "192.168.1.11/24")
-log-level string
min level of logs to print (default "info")
-mac string
mac address of the interface inside the namespace (default will be a random one)
-ns-path string
path of the temporary namespace to be created (default "/var/run/netns/w000t$PID")
Expand Down
24 changes: 17 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/vishvananda/netns"
)

var ip, command, gateway, intf, logLevel, nsPath string
var ip, command, gateway, intf, logLevel, nsPath, mac string
var log = logrus.New()

func init() {
Expand All @@ -26,6 +26,7 @@ func init() {
flag.StringVar(&command, "command", "ip route", "command to be executed")
flag.StringVar(&gateway, "gw", "", "gateway of the request (default will be the default route of the given interface)")
flag.StringVar(&logLevel, "log-level", "info", "min level of logs to print")
flag.StringVar(&mac, "mac", "", "mac address of the interface inside the namespace (default will be a random one)")
flag.StringVar(
&nsPath,
"ns-path",
Expand Down Expand Up @@ -120,19 +121,28 @@ func main() {
return
}

err = netlink.LinkSetDown(macVlan)
if err != nil {
log.Warn("Error while setting macVlan down: ", err)
return
}

link, err := netlink.LinkByName("peth0")
if err != nil {
log.Warn("Error while getting macVlan: ", err)
return
}
log.Debugf("MacVlan created : %+v", link)

// If a mac was specified, set it now
if mac != "" {
log.Debugf("Setting macVlan with specified MAC : %s", mac)
hardwareAddr, err := net.ParseMAC(mac)
if err != nil {
log.Warn("Error while parsing given mac: ", err)
return
}
err = netlink.LinkSetHardwareAddr(link, hardwareAddr)
if err != nil {
log.Warn("Error while setting given mac on macVlan: ", err)
return
}
}

// ============================== Create the new Namespace

newns, err := newNS()
Expand Down

0 comments on commit 34efd52

Please sign in to comment.