|
| 1 | +package veth |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/Alonza0314/nsctl/namespace" |
| 8 | + "github.com/vishvananda/netlink" |
| 9 | + "github.com/vishvananda/netns" |
| 10 | +) |
| 11 | + |
| 12 | +func List(ns string) (string, error) { |
| 13 | + _, originCloseFunc, err := namespace.GetOriginNs() |
| 14 | + if err != nil { |
| 15 | + return "", err |
| 16 | + } |
| 17 | + defer originCloseFunc() |
| 18 | + |
| 19 | + targetNsFd, targetNsCloseFunc, err := namespace.GetNsFd(ns) |
| 20 | + if err != nil { |
| 21 | + return "", fmt.Errorf("failed to namespace %s file descriptor: %v", ns, err) |
| 22 | + } |
| 23 | + defer targetNsCloseFunc() |
| 24 | + |
| 25 | + if err := netns.Set(targetNsFd); err != nil { |
| 26 | + return "", fmt.Errorf("failed to set to namespace %s: %v", ns, err) |
| 27 | + } |
| 28 | + |
| 29 | + links, err := netlink.LinkList() |
| 30 | + if err != nil { |
| 31 | + return "", fmt.Errorf("failed to get net links in namespace %s: %v", ns, err) |
| 32 | + } |
| 33 | + |
| 34 | + return printTable(links), nil |
| 35 | +} |
| 36 | + |
| 37 | +func printTable(links []netlink.Link) string { |
| 38 | + var b strings.Builder |
| 39 | + |
| 40 | + ifWidth, stateWidth, ipWidth, macWidth := 18, 5, 18, 17 |
| 41 | + |
| 42 | + b.WriteString("┌" + strings.Repeat("─", ifWidth) + "┬" + strings.Repeat("─", stateWidth) + "┬" + strings.Repeat("─", ipWidth) + "┬" + strings.Repeat("─", macWidth) + "┐\n") |
| 43 | + b.WriteString(fmt.Sprintf("│%-*.*s│%-*.*s│%-*.*s│%-*.*s│\n", ifWidth, ifWidth, "IFACE", stateWidth, stateWidth, "STATE", ipWidth, ipWidth, "IP", macWidth, macWidth, "MAC")) |
| 44 | + b.WriteString("├" + strings.Repeat("─", ifWidth) + "┼" + strings.Repeat("─", stateWidth) + "┼" + strings.Repeat("─", ipWidth) + "┼" + strings.Repeat("─", macWidth) + "┤\n") |
| 45 | + |
| 46 | + for _, link := range links { |
| 47 | + attrs := link.Attrs() |
| 48 | + if attrs == nil { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + if attrs.Name == "gre0" || attrs.Name == "gretap0" || attrs.Name == "erspan0" || attrs.Name == "ip6gre0" || attrs.Name == "ip6gretap0" { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + state := attrs.OperState.String() |
| 57 | + if state == "" { |
| 58 | + state = "-" |
| 59 | + } |
| 60 | + |
| 61 | + ip := "-" |
| 62 | + if addrs, err := netlink.AddrList(link, netlink.FAMILY_ALL); err == nil && len(addrs) > 0 { |
| 63 | + ipList := make([]string, 0, len(addrs)) |
| 64 | + for _, addr := range addrs { |
| 65 | + if addr.IPNet != nil { |
| 66 | + ipList = append(ipList, addr.IPNet.IP.String()) |
| 67 | + } |
| 68 | + } |
| 69 | + if len(ipList) > 0 { |
| 70 | + ip = strings.Join(ipList, ",") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + mac := attrs.HardwareAddr.String() |
| 75 | + if mac == "" { |
| 76 | + mac = "-" |
| 77 | + } |
| 78 | + |
| 79 | + b.WriteString(fmt.Sprintf("│%-*.*s│%-*.*s│%-*.*s│%-*.*s│\n", ifWidth, ifWidth, attrs.Name, stateWidth, stateWidth, state, ipWidth, ipWidth, ip, macWidth, macWidth, mac)) |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + b.WriteString("└" + strings.Repeat("─", ifWidth) + "┴" + strings.Repeat("─", stateWidth) + "┴" + strings.Repeat("─", ipWidth) + "┴" + strings.Repeat("─", macWidth) + "┘\n") |
| 84 | + |
| 85 | + return b.String() |
| 86 | +} |
0 commit comments