Skip to content

Instantly share code, notes, and snippets.

@mangalaman93
Created October 11, 2015 03:02
Show Gist options
  • Save mangalaman93/3b95963ef4365684511d to your computer and use it in GitHub Desktop.
Save mangalaman93/3b95963ef4365684511d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"syscall"
)
const (
cont_id = "3d92b3b1bdb2"
pid = 7492
)
func getNS(cont_id string, pid int) string {
// try /var/run/netns -> for novadocker containers
filepath := path.Join("/var/run/netns/", cont_id)
if _, err := os.Stat(filepath); err == nil {
return filepath
}
// docker containers
filepath = path.Join("/proc/", strconv.Itoa(pid), "/ns/net")
if _, err := os.Stat(filepath); err == nil {
return filepath
}
return ""
}
func main() {
// change namespace
ns := getNS(cont_id, pid)
if "" == ns {
fmt.Println("[ERROR] unable to figure out ns for container")
panic("ns not found")
}
fd, err := syscall.Open(ns, syscall.O_RDONLY, 0666)
if err != nil {
fmt.Println("[ERROR] unable to open ns file for container")
panic(err)
}
defer syscall.Close(fd)
if _, _, err := syscall.RawSyscall(308, uintptr(fd), 0, 0); err != 0 {
fmt.Println("[ERROR] unable to change namespace for container")
panic(err)
}
out, err := ioutil.ReadFile("/proc/net/udp")
fmt.Println(string(out))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment