Skip to content

Instantly share code, notes, and snippets.

@chenchun
Created October 22, 2015 04:50
Show Gist options
  • Save chenchun/2e73dd5a27d9a2959765 to your computer and use it in GitHub Desktop.
Save chenchun/2e73dd5a27d9a2959765 to your computer and use it in GitHub Desktop.
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