Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new flag -D: Print the list of the network interfaces available on the system #53

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@ Flags:
| --print | ✅ | ✅ |
| -c *count* | ✅ | ✅ |
| -Q *direction*, --direction=*direction* | ✅ | ✅ |
| --list-interfaces | ✅ | ✅ |
| -D, --list-interfaces | ✅ | ✅ |
| -A | ✅ | |
| -B *bufer_size*, --buffer-size=*buffer_size* | ✅ | |
| --count | ✅ | ✅ |
| -C *file_size | ✅ | |
| -d | ✅ | |
| -dd | ✅ | |
| -ddd | ✅ | |
| -D | ✅ | |
| -e | ✅ | |
| -f | ✅ | ⛔ |
| -F *file* | ✅ | |
Expand Down
15 changes: 12 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@ package cmd

import (
"fmt"
"github.com/mozillazg/ptcpdump/internal/dev"
"sort"
"strings"

"github.com/mozillazg/ptcpdump/internal/dev"
)

func listInterfaces() error {
devices, err := dev.GetDevices(nil)
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
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading