Created
October 22, 2015 04:50
-
-
Save chenchun/2e73dd5a27d9a2959765 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"runtime" | |
"fmt" | |
"github.com/vishvananda/netns" | |
"os" | |
) | |
func main() { | |
// Lock the OS Thread so we don't accidentally switch namespaces | |
runtime.LockOSThread() | |
defer runtime.UnlockOSThread() | |
file := "/var/run/docker/netns/1-107d5fc995" | |
f, err := os.OpenFile(file, os.O_RDONLY, 0) | |
if err != nil { | |
exit("Failed to open network namespace path %q: %v \n", file, err) | |
} | |
defer f.Close() | |
if err = netns.Set(netns.NsHandle(f.Fd())); err != nil { | |
exit("Setting setns %s failed: %v \n", file, err) | |
} | |
ifaces, _ := net.Interfaces() | |
fmt.Printf("Interfaces: %v\n", ifaces) | |
} | |
func exit(format string, a ...interface{}) { | |
fmt.Printf(format, a...) | |
fmt.Println() | |
os.Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment