From 11c4045ea9d20fb9a4ef5e7d86eb751a8c3a2bdd Mon Sep 17 00:00:00 2001 From: mozillazg Date: Sun, 9 Jun 2024 18:04:29 +0800 Subject: [PATCH] add new flag `-D`: Print the list of the network interfaces available on the system --- README.md | 3 +-- cmd/list.go | 15 ++++++++++++--- cmd/root.go | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ad7c1f7b..b2f4426e 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ Flags: | --print | ✅ | ✅ | | -c *count* | ✅ | ✅ | | -Q *direction*, --direction=*direction* | ✅ | ✅ | -| --list-interfaces | ✅ | ✅ | +| -D, --list-interfaces | ✅ | ✅ | | -A | ✅ | | | -B *bufer_size*, --buffer-size=*buffer_size* | ✅ | | | --count | ✅ | ✅ | @@ -152,7 +152,6 @@ Flags: | -d | ✅ | | | -dd | ✅ | | | -ddd | ✅ | | -| -D | ✅ | | | -e | ✅ | | | -f | ✅ | ⛔ | | -F *file* | ✅ | | diff --git a/cmd/list.go b/cmd/list.go index bad4412a..62582ace 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -2,9 +2,10 @@ package cmd import ( "fmt" - "github.com/mozillazg/ptcpdump/internal/dev" "sort" "strings" + + "github.com/mozillazg/ptcpdump/internal/dev" ) func listInterfaces() error { @@ -12,11 +13,19 @@ func listInterfaces() error { if err != nil { return err } - outputs := []string{} + var interfaces []dev.Device for _, d := range devices { + interfaces = append(interfaces, d) + } + sort.Slice(interfaces, func(i, j int) bool { + return interfaces[i].Ifindex < interfaces[j].Ifindex + }) + + outputs := []string{} + for _, d := range interfaces { outputs = append(outputs, fmt.Sprintf("%d.%s", d.Ifindex, d.Name)) } - sort.Strings(outputs) + fmt.Printf("%s\n", strings.Join(outputs, "\n")) return nil } diff --git a/cmd/root.go b/cmd/root.go index 76c83fd3..c20b9205 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -51,7 +51,7 @@ func init() { rootCmd.Flags().StringVar(&opts.comm, "pname", "", "Filter by process name (only TCP and UDP packets are supported)") rootCmd.Flags().BoolVarP(&opts.followForks, "follow-forks", "f", false, "Trace child processes as they are created by currently traced processes when filter by process") - rootCmd.Flags().BoolVar(&opts.listInterfaces, "list-interfaces", false, + rootCmd.Flags().BoolVarP(&opts.listInterfaces, "list-interfaces", "D", false, "Print the list of the network interfaces available on the system") rootCmd.Flags().BoolVar(&opts.version, "version", false, "Print the ptcpdump and libpcap version strings and exit")