This commit is contained in:
root 2025-06-25 14:22:34 -04:00
parent 853c0e6357
commit 86cbb31a2f
2 changed files with 39 additions and 45 deletions

BIN
main

Binary file not shown.

34
main.go
View File

@ -4,9 +4,7 @@ import (
"fmt"
"io/ioutil"
"net"
//"os"
"path/filepath"
//"strings"
"github.com/charmbracelet/lipgloss"
"github.com/vishvananda/netlink"
@ -22,14 +20,15 @@ func main() {
addrStyle := lipgloss.NewStyle().Faint(true)
infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("33"))
// Save the current netns
// Save the current namespace
origNS, err := netns.Get()
if err != nil {
fmt.Println("Error getting current netns:", err)
fmt.Println("Error getting current namespace:", err)
return
}
defer origNS.Close()
// Get all links in the current namespace
links, err := netlink.LinkList()
if err != nil {
fmt.Println("Error listing links:", err)
@ -52,7 +51,7 @@ func main() {
fmt.Printf(addrStyle.Render(" %d"), attrs.MTU)
fmt.Printf(" %s\n", state)
// IP addresses (IPv4 only)
// IPv4 only
addrs, err := netlink.AddrList(link, unix.AF_INET)
if err == nil {
for _, addr := range addrs {
@ -67,21 +66,7 @@ func main() {
fmt.Println(" ", infoStyle.Render("Veth interface"))
// Try to find peer locally
peerFound := false
for _, other := range links {
if other.Attrs().Index == attrs.Index {
continue
}
if other.Type() == "veth" && other.Attrs().ParentIndex == attrs.Index {
fmt.Println(" ", infoStyle.Render("Likely peer:"), other.Attrs().Name)
peerFound = true
break
}
}
// If not found, scan /var/run/netns/
if !peerFound {
netnsDir := "/var/run/netns/"
entries, err := ioutil.ReadDir(netnsDir)
if err != nil {
@ -105,11 +90,21 @@ func main() {
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)
// Get peer IPs inside this namespace
peerAddrs, err := netlink.AddrList(rl, unix.AF_INET)
if err == nil {
for _, pa := range peerAddrs {
fmt.Println(" ", ipStyle.Render(pa.IPNet.String()))
}
}
peerFound = true
break
}
}
}
nsHandle.Close()
netns.Set(origNS)
@ -118,7 +113,6 @@ func main() {
}
}
}
}
if !peerFound {
fmt.Println(" ", infoStyle.Render("Peer not found — may be deleted or hidden"))