Skip to content

Commit 1480d53

Browse files
author
shadowy-pycoder
committed
Small enhancements for the cli
1 parent 7f9cd67 commit 1480d53

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

cmd/mshark/cli.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"os"
88
"strings"
9+
"text/tabwriter"
910

1011
ms "github.com/shadowy-pycoder/mshark"
1112
)
@@ -31,15 +32,18 @@ Options:
3132
`
3233

3334
func displayInterfaces() error {
35+
w := new(tabwriter.Writer)
36+
w.Init(os.Stdout, 0, 0, 2, ' ', tabwriter.TabIndent)
3437
ifaces, err := net.Interfaces()
3538
if err != nil {
3639
return fmt.Errorf("failed to get network interfaces: %v", err)
3740
}
38-
fmt.Println("0. any")
41+
fmt.Fprintln(w, "Index\tName\tFlags")
42+
fmt.Fprintln(w, "0\tany\tUP")
3943
for _, iface := range ifaces {
40-
fmt.Printf("%d. %s %s\n", iface.Index, iface.Name, strings.ToUpper(iface.Flags.String()))
44+
fmt.Fprintf(w, "%d\t%s\t%s\n", iface.Index, iface.Name, strings.ToUpper(iface.Flags.String()))
4145
}
42-
return nil
46+
return w.Flush()
4347
}
4448

4549
func root(args []string) error {
@@ -48,19 +52,18 @@ func root(args []string) error {
4852
flags := flag.NewFlagSet("mshark", flag.ExitOnError)
4953
flags.StringVar(&conf.Iface, "i", "any", "The name of the network interface. Example: eth0")
5054
flags.IntVar(&conf.Snaplen, "s", 0, "The maximum length of each packet snapshot. Defaults to 65535.")
51-
flags.BoolFunc("p", `Promiscuous mode. This setting is ignored for "any" interface.`, func(flagValue string) error {
55+
flags.BoolFunc("p", `Promiscuous mode. This setting is ignored for "any" interface. Defaults to false.`, func(flagValue string) error {
5256
conf.Promisc = true
5357
return nil
5458
})
5559
flags.DurationVar(&conf.Timeout, "t", 0, "The maximum duration of the packet capture process. Example: 5s")
5660
flags.IntVar(&conf.PacketCount, "c", 0, "The maximum number of packets to capture.")
5761
flags.StringVar(&conf.Expr, "e", "", `BPF filter expression. Example: "ip proto tcp"`)
58-
flags.StringVar(&conf.Path, "f", "", "File path to write captured packet data to. Example: ./captured.txt")
59-
flags.BoolFunc("pcap", "Whether to create PCAP file.", func(flagValue string) error {
62+
flags.StringVar(&conf.File, "f", "", "File path to write captured packet data to. Defaults to stdout.")
63+
flags.BoolFunc("pcap", "Create a PCAP file in the current working directory.", func(flagValue string) error {
6064
conf.Pcap = true
6165
return nil
6266
})
63-
flags.StringVar(&conf.PcapPath, "path", "", "Path to a PCAP file. Example: ./captured.pcap")
6467
flags.BoolFunc("D", "Display list of interfaces and exit.", func(flagValue string) error {
6568
if err := displayInterfaces(); err != nil {
6669
fmt.Fprintf(os.Stderr, "mshark: %v\n", err)

mshark.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ type Config struct {
3333
Timeout time.Duration // The maximum duration of the packet capture process.
3434
PacketCount int // The maximum number of packets to capture.
3535
Expr string // BPF filter expression.
36-
Path string // File path to write captured packet data to.
37-
Pcap bool // Whether to create PCAP file.
38-
PcapPath string // Path to a PCAP file. Defaults to "mshark_timestamp.pcap" in the current working directory.
36+
File string // File path to write captured packet data to. Defaults to /dev/stdout
37+
Pcap bool // Create a PCAP file in the current working directory.
3938
}
4039

4140
func OpenLive(conf *Config) error {
@@ -110,14 +109,14 @@ func OpenLive(conf *Config) error {
110109

111110
// file to write packets
112111
var f *os.File
113-
if conf.Path == "" {
114-
f = os.Stdout
115-
} else {
116-
f, err = os.OpenFile(filepath.FromSlash(conf.Path), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
112+
if conf.File != "" {
113+
f, err = os.OpenFile(filepath.FromSlash(conf.File), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
117114
if err != nil {
118115
return fmt.Errorf("failed to open file: %v", err)
119116
}
120117
defer f.Close()
118+
} else {
119+
f = os.Stdout
121120
}
122121

123122
// number of packets
@@ -162,10 +161,8 @@ func OpenLive(conf *Config) error {
162161
// pcap file to write packets
163162
var pcap *mpcap.PcapWriter
164163
if conf.Pcap {
165-
if conf.PcapPath == "" {
166-
conf.PcapPath = fmt.Sprintf("./mshark_%s.pcap", time.Now().UTC().Format("20060102_150405"))
167-
}
168-
fpcap, err := os.OpenFile(filepath.FromSlash(conf.PcapPath), os.O_CREATE|os.O_WRONLY, 0644)
164+
path := fmt.Sprintf("./mshark_%s.pcap", time.Now().UTC().Format("20060102_150405"))
165+
fpcap, err := os.OpenFile(filepath.FromSlash(path), os.O_CREATE|os.O_WRONLY, 0644)
169166
if err != nil {
170167
return fmt.Errorf("failed to open file: %v", err)
171168
}

mshark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func BenchmarkOpenLive(b *testing.B) {
1212
Snaplen: 1600,
1313
Promisc: true,
1414
PacketCount: b.N,
15-
Path: os.DevNull,
15+
File: os.DevNull,
1616
}
1717
if err := OpenLive(&conf); err != nil {
1818
b.Fatal(err)

0 commit comments

Comments
 (0)