From b933d2252d65f67668559a749d7bfd8196935409 Mon Sep 17 00:00:00 2001 From: nori Seokju Yun Date: Sun, 21 Aug 2016 01:53:35 +0900 Subject: [PATCH 1/4] Update ListDeviceNames function to return IP address It is helpful to set up a sniffer interface. --- packetbeat/sniffer/sniffer.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/packetbeat/sniffer/sniffer.go b/packetbeat/sniffer/sniffer.go index fe787508140..2920a3d0923 100644 --- a/packetbeat/sniffer/sniffer.go +++ b/packetbeat/sniffer/sniffer.go @@ -74,8 +74,9 @@ func deviceNameFromIndex(index int, devices []string) (string, error) { // ListDevicesNames returns the list of adapters available for sniffing on // this computer. If the withDescription parameter is set to true, a human -// readable version of the adapter name is added. -func ListDeviceNames(withDescription bool) ([]string, error) { +// readable version of the adapter name is added. If the withIP parameter +// is set to true, IP address of the adatper is added. +func ListDeviceNames(withDescription bool, withIP bool) ([]string, error) { devices, err := pcap.FindAllDevs() if err != nil { return []string{}, err @@ -83,15 +84,34 @@ func ListDeviceNames(withDescription bool) ([]string, error) { ret := []string{} for _, dev := range devices { + result := dev.Name + if withDescription { desc := "No description available" if len(dev.Description) > 0 { desc = dev.Description } - ret = append(ret, fmt.Sprintf("%s (%s)", dev.Name, desc)) - } else { - ret = append(ret, dev.Name) + result += fmt.Sprintf(" (%s)", desc) + } + + if withIP { + ips := "Not assigned ip address" + if len(dev.Addresses) > 0 { + ips = "" + + for index, address := range []pcap.InterfaceAddress(dev.Addresses) { + if index > 0 { + // add a space between the IP address. + ips += " " + } + + ips += fmt.Sprintf("%s", address.IP.String()) + } + } + result += fmt.Sprintf(" (%s)", ips) + } + ret = append(ret, result) } return ret, nil } From 9718c2deef4dd671dfc0ace25ae86d2dacf95c7e Mon Sep 17 00:00:00 2001 From: nori Seokju Yun Date: Sun, 21 Aug 2016 01:55:43 +0900 Subject: [PATCH 2/4] Apply ListDeviceNames parameter changes --- packetbeat/beater/devices.go | 2 +- packetbeat/sniffer/sniffer.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packetbeat/beater/devices.go b/packetbeat/beater/devices.go index 85a86d151b1..59c280d1e84 100644 --- a/packetbeat/beater/devices.go +++ b/packetbeat/beater/devices.go @@ -18,7 +18,7 @@ func init() { return nil } - devs, err := sniffer.ListDeviceNames(true) + devs, err := sniffer.ListDeviceNames(true, true) if err != nil { return fmt.Errorf("Error getting devices list: %v\n", err) } diff --git a/packetbeat/sniffer/sniffer.go b/packetbeat/sniffer/sniffer.go index 2920a3d0923..c0d98f7898a 100644 --- a/packetbeat/sniffer/sniffer.go +++ b/packetbeat/sniffer/sniffer.go @@ -133,7 +133,7 @@ func (sniffer *SnifferSetup) setFromConfig(config *config.InterfacesConfig) erro } if index, err := strconv.Atoi(sniffer.config.Device); err == nil { // Device is numeric - devices, err := ListDeviceNames(false) + devices, err := ListDeviceNames(false, false) if err != nil { return fmt.Errorf("Error getting devices list: %v", err) } From 784e752251cc3196c3d77fc1877213e84904e0a7 Mon Sep 17 00:00:00 2001 From: nori Seokju Yun Date: Tue, 23 Aug 2016 23:37:24 +0900 Subject: [PATCH 3/4] Apply golang coding guidelines --- packetbeat/sniffer/sniffer.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packetbeat/sniffer/sniffer.go b/packetbeat/sniffer/sniffer.go index c0d98f7898a..3a7ddc4d336 100644 --- a/packetbeat/sniffer/sniffer.go +++ b/packetbeat/sniffer/sniffer.go @@ -84,34 +84,34 @@ func ListDeviceNames(withDescription bool, withIP bool) ([]string, error) { ret := []string{} for _, dev := range devices { - result := dev.Name - + r := dev.Name + if withDescription { desc := "No description available" if len(dev.Description) > 0 { desc = dev.Description } - result += fmt.Sprintf(" (%s)", desc) + r += fmt.Sprintf(" (%s)", desc) } - + if withIP { ips := "Not assigned ip address" if len(dev.Addresses) > 0 { ips = "" - - for index, address := range []pcap.InterfaceAddress(dev.Addresses) { - if index > 0 { - // add a space between the IP address. + + for i, address := range []pcap.InterfaceAddress(dev.Addresses) { + // Add a space between the IP address. + if i > 0 { ips += " " } - + ips += fmt.Sprintf("%s", address.IP.String()) } } - result += fmt.Sprintf(" (%s)", ips) + r += fmt.Sprintf(" (%s)", ips) } - ret = append(ret, result) + ret = append(ret, r) } return ret, nil } From 73c63af618c7722509048b62b048994add73900e Mon Sep 17 00:00:00 2001 From: nori Seokju Yun Date: Tue, 23 Aug 2016 23:47:27 +0900 Subject: [PATCH 4/4] Add -device command enhancement to a changelog. --- CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index ee256c4f6c1..d478831ae1c 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -86,6 +86,7 @@ https://github.com/elastic/beats/compare/v5.0.0-alpha5...master[Check the HEAD d - Add cassandra protocol analyzer to packetbeat. {pull}1959[1959] - Match connections with IPv6 addresses to processes {pull}2254[2254] + - Add IP address to -devices command output {pull}2327[2327] *Topbeat*