diff --git a/README.md b/README.md index 582c0e9..52dd077 100644 --- a/README.md +++ b/README.md @@ -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") diff --git a/main.go b/main.go index 29bbfc2..b462d1c 100644 --- a/main.go +++ b/main.go @@ -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() { @@ -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", @@ -120,12 +121,6 @@ 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) @@ -133,6 +128,21 @@ func main() { } 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()