Skip to content

Commit 7f9cd67

Browse files
author
shadowy-pycoder
committed
Added printing the list of all network interfaces
1 parent d4cdcea commit 7f9cd67

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cmd/mshark/cli.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"net"
7+
"os"
8+
"strings"
69

710
ms "github.com/shadowy-pycoder/mshark"
811
)
@@ -27,6 +30,18 @@ Options:
2730
-h Show this help message and exit.
2831
`
2932

33+
func displayInterfaces() error {
34+
ifaces, err := net.Interfaces()
35+
if err != nil {
36+
return fmt.Errorf("failed to get network interfaces: %v", err)
37+
}
38+
fmt.Println("0. any")
39+
for _, iface := range ifaces {
40+
fmt.Printf("%d. %s %s\n", iface.Index, iface.Name, strings.ToUpper(iface.Flags.String()))
41+
}
42+
return nil
43+
}
44+
3045
func root(args []string) error {
3146
conf := ms.Config{}
3247

@@ -46,6 +61,14 @@ func root(args []string) error {
4661
return nil
4762
})
4863
flags.StringVar(&conf.PcapPath, "path", "", "Path to a PCAP file. Example: ./captured.pcap")
64+
flags.BoolFunc("D", "Display list of interfaces and exit.", func(flagValue string) error {
65+
if err := displayInterfaces(); err != nil {
66+
fmt.Fprintf(os.Stderr, "mshark: %v\n", err)
67+
os.Exit(2)
68+
}
69+
os.Exit(0)
70+
return nil
71+
})
4972

5073
flags.Usage = func() {
5174
fmt.Print(usagePrefix)

0 commit comments

Comments
 (0)