This commit is contained in:
root 2025-06-25 14:17:45 -04:00
parent 180805ed64
commit 853c0e6357
2 changed files with 77 additions and 32 deletions

BIN
main

Binary file not shown.

83
main.go
View File

@ -2,15 +2,19 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"net" "net"
//"os"
"path/filepath"
//"strings"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
func main() { func main() {
// Styles
headerStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("63")) headerStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("63"))
ipStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("203")) ipStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("203"))
upStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("42")) upStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("42"))
@ -18,7 +22,14 @@ func main() {
addrStyle := lipgloss.NewStyle().Faint(true) addrStyle := lipgloss.NewStyle().Faint(true)
infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("33")) infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("33"))
// Get all links // Save the current netns
origNS, err := netns.Get()
if err != nil {
fmt.Println("Error getting current netns:", err)
return
}
defer origNS.Close()
links, err := netlink.LinkList() links, err := netlink.LinkList()
if err != nil { if err != nil {
fmt.Println("Error listing links:", err) fmt.Println("Error listing links:", err)
@ -31,36 +42,32 @@ func main() {
continue continue
} }
// Link state
state := downStyle.Render("DOWN") state := downStyle.Render("DOWN")
if attrs.Flags&net.FlagUp != 0 { if attrs.Flags&net.FlagUp != 0 {
state = upStyle.Render("UP") state = upStyle.Render("UP")
} }
// Interface header
fmt.Print(headerStyle.Render(attrs.Name), " ") fmt.Print(headerStyle.Render(attrs.Name), " ")
fmt.Printf(addrStyle.Render(" %s"), attrs.HardwareAddr) fmt.Printf(addrStyle.Render(" %s"), attrs.HardwareAddr)
fmt.Printf(addrStyle.Render(" %d"), attrs.MTU) fmt.Printf(addrStyle.Render(" %d"), attrs.MTU)
fmt.Printf(" %s\n", state) fmt.Printf(" %s\n", state)
// IPv4 addresses // IP addresses (IPv4 only)
addrs, err := netlink.AddrList(link, unix.AF_INET) addrs, err := netlink.AddrList(link, unix.AF_INET)
if err != nil { if err == nil {
fmt.Println(" Error getting addresses:", err)
continue
}
for _, addr := range addrs { for _, addr := range addrs {
fmt.Println(" ", ipStyle.Render(addr.IPNet.String())) fmt.Println(" ", ipStyle.Render(addr.IPNet.String()))
} }
}
if link.Type() != "veth" {
fmt.Println()
continue
}
// Veth handling
if link.Type() == "veth" {
fmt.Println(" ", infoStyle.Render("Veth interface")) fmt.Println(" ", infoStyle.Render("Veth interface"))
// Try to find peer by looking for other veths with same parent or hint from name // Try to find peer locally
// No peer name is available here, so we must guess
// We'll mention that if no matching veth exists, it's likely in another netns
peerFound := false peerFound := false
for _, other := range links { for _, other := range links {
if other.Attrs().Index == attrs.Index { if other.Attrs().Index == attrs.Index {
@ -73,13 +80,51 @@ func main() {
} }
} }
// If not found, scan /var/run/netns/
if !peerFound { if !peerFound {
fmt.Println(" ", infoStyle.Render("Peer likely in another namespace")) netnsDir := "/var/run/netns/"
} entries, err := ioutil.ReadDir(netnsDir)
} if err != nil {
fmt.Println() fmt.Println(" ", infoStyle.Render("Could not scan netns dir"))
} else {
for _, entry := range entries {
nsPath := filepath.Join(netnsDir, entry.Name())
nsHandle, err := netns.GetFromPath(nsPath)
if err != nil {
continue
}
if err := netns.Set(nsHandle); err != nil {
nsHandle.Close()
continue
}
remoteLinks, err := netlink.LinkList()
if err == nil {
for _, rl := range remoteLinks {
if rl.Type() == "veth" && rl.Attrs().ParentIndex == attrs.Index {
fmt.Println(" ", infoStyle.Render("Peer in namespace:"), entry.Name(), "as", rl.Attrs().Name)
peerFound = true
break
}
}
}
nsHandle.Close()
netns.Set(origNS)
if peerFound {
break
}
}
}
}
if !peerFound {
fmt.Println(" ", infoStyle.Render("Peer not found — may be deleted or hidden"))
}
fmt.Println()
} }
} }