diff --git a/nsinit/main.go b/nsinit/main.go index bd944b82..96f37be9 100644 --- a/nsinit/main.go +++ b/nsinit/main.go @@ -22,6 +22,11 @@ func init() { Usage: "mknod a device inside an existing container", Action: nsenterMknod, } + + argvs["nsenter-ip"] = &rFunc{ + Usage: "display the container's network interfaces", + Action: nsenterIp, + } } func preload(context *cli.Context) error { diff --git a/nsinit/nsenter.go b/nsinit/nsenter.go index 92c73c74..8dc149f4 100644 --- a/nsinit/nsenter.go +++ b/nsinit/nsenter.go @@ -1,8 +1,13 @@ package main import ( + "fmt" "log" + "net" + "os" "strconv" + "strings" + "text/tabwriter" "github.com/docker/libcontainer" "github.com/docker/libcontainer/devices" @@ -49,3 +54,31 @@ func nsenterMknod(config *libcontainer.Config, args []string) { log.Fatal(err) } } + +// nsenterIp displays the network interfaces inside a container's net namespace +func nsenterIp(config *libcontainer.Config, args []string) { + interfaces, err := net.Interfaces() + if err != nil { + log.Fatal(err) + } + + w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0) + fmt.Fprint(w, "NAME\tMTU\tMAC\tFLAG\tADDRS\n") + + for _, iface := range interfaces { + addrs, err := iface.Addrs() + if err != nil { + log.Fatal(err) + } + + o := []string{} + + for _, a := range addrs { + o = append(o, a.String()) + } + + fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\n", iface.Name, iface.MTU, iface.HardwareAddr, iface.Flags, strings.Join(o, ",")) + } + + w.Flush() +}