iter 3
This commit is contained in:
parent
180805ed64
commit
853c0e6357
109
main.go
109
main.go
|
@ -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,55 +42,89 @@ 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)
|
for _, addr := range addrs {
|
||||||
|
fmt.Println(" ", ipStyle.Render(addr.IPNet.String()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if link.Type() != "veth" {
|
||||||
|
fmt.Println()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, addr := range addrs {
|
|
||||||
fmt.Println(" ", ipStyle.Render(addr.IPNet.String()))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
fmt.Println(" ", infoStyle.Render("Veth interface"))
|
||||||
|
|
||||||
// Veth handling
|
// Try to find peer locally
|
||||||
if link.Type() == "veth" {
|
peerFound := false
|
||||||
fmt.Println(" ", infoStyle.Render("Veth interface"))
|
for _, other := range links {
|
||||||
|
if other.Attrs().Index == attrs.Index {
|
||||||
// Try to find peer by looking for other veths with same parent or hint from name
|
continue
|
||||||
// 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
|
|
||||||
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 other.Type() == "veth" && other.Attrs().ParentIndex == attrs.Index {
|
||||||
if !peerFound {
|
fmt.Println(" ", infoStyle.Render("Likely peer:"), other.Attrs().Name)
|
||||||
fmt.Println(" ", infoStyle.Render("Peer likely in another namespace"))
|
peerFound = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If not found, scan /var/run/netns/
|
||||||
|
if !peerFound {
|
||||||
|
netnsDir := "/var/run/netns/"
|
||||||
|
entries, err := ioutil.ReadDir(netnsDir)
|
||||||
|
if err != nil {
|
||||||
|
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()
|
fmt.Println()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue