@@ -24,13 +24,13 @@ func init() {
24
24
flag .StringVar (& ip , "ip" , "192.168.1.11/24" , "IP network from where the command will be executed" )
25
25
flag .StringVar (& intf , "interface" , "eth0" , "interface used to get out of the network" )
26
26
flag .StringVar (& command , "command" , "ip route" , "command to be executed" )
27
- flag .StringVar (& gateway , "gw" , "" , "gateway of the request" )
27
+ flag .StringVar (& gateway , "gw" , "" , "gateway of the request (default will be the default route of the given interface) " )
28
28
flag .StringVar (& logLevel , "log-level" , "info" , "min level of logs to print" )
29
29
flag .StringVar (
30
30
& nsPath ,
31
31
"ns-path" ,
32
- fmt . Sprintf ( "/var/run/netns/w000t%d" , os . Getpid ()) ,
33
- "path of the temporary namespace to be created, default will be /var/run/netns/w000t$PID" ,
32
+ "" ,
33
+ "path of the temporary namespace to be created ( default will be /var/run/netns/w000t$PID) " ,
34
34
)
35
35
flag .Parse ()
36
36
}
@@ -75,6 +75,33 @@ func main() {
75
75
}
76
76
log .Debugf ("%s : %+v" , intf , eth .Attrs ().Flags )
77
77
78
+ // If no nsPath is given, we'll use one named like
79
+ // /var/run/netns/w000t$PID
80
+ if nsPath == "" {
81
+ nsPath = fmt .Sprintf ("/var/run/netns/w000t%d" , os .Getpid ())
82
+ }
83
+
84
+ // If no gateway is specified, we'll use the first route of the given interface
85
+ if gateway == "" {
86
+ routes , err := netlink .RouteList (eth , netlink .FAMILY_V4 )
87
+ if err != nil {
88
+ log .Warn ("Failed to get the route of the interface: " , err )
89
+ return
90
+ }
91
+
92
+ for _ , r := range routes {
93
+ if r .Gw != nil {
94
+ gateway = r .Gw .String ()
95
+ break
96
+ }
97
+ }
98
+ if gateway == "" {
99
+ log .Warnf ("Couldn't find a default gateway for the specified interface" )
100
+ return
101
+ }
102
+ }
103
+ gwaddr := net .ParseIP (gateway )
104
+
78
105
// ============================== Create the macVLAN
79
106
80
107
log .Debug ("Create a new macVlan" )
@@ -156,7 +183,6 @@ func main() {
156
183
log .Warn ("Failed to add the IP to the macVlan: " , err )
157
184
return
158
185
}
159
- gwaddr := net .ParseIP (gateway )
160
186
161
187
log .Debug ("Set the macVlan interface UP" )
162
188
// ============================= Set the link up in the namespace
0 commit comments