From 24749133dc7045634c5096eee9d3733b3d7d31ff Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 12 Feb 2018 22:49:46 +0900 Subject: [PATCH] :+1: Add interface flag --- main.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index ddeb368..e0e70ae 100644 --- a/main.go +++ b/main.go @@ -19,15 +19,33 @@ var ( handle *pcap.Handle ) -func main() { - var device string = "lo" +func findDevice(c *cli.Context) string { + if c.String("interface") != "" { + return c.String("interface") + } + devices, err := pcap.FindAllDevs() + if err != nil { + panic(err) + } + + return devices[0].Name +} +func main() { app := cli.NewApp() app.Name = "tcpterm" app.Usage = "tcpdump for human" app.Version = "0.0.1" + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "interface, i", + Usage: "If unspecified, use lowest numbered interface.", + }, + } + app.Action = func(c *cli.Context) error { + device := findDevice(c) handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout) if err != nil { log.Fatal(err)