Skip to content

Commit

Permalink
Add IP address to -devices command output (#2327)
Browse files Browse the repository at this point in the history
It is helpful to set up a sniffer interface.

* Update ListDeviceNames function to return IP address
* Apply ListDeviceNames parameter changes
* Add -device command enhancement to a changelog.
  • Loading branch information
noricube authored and ruflin committed Aug 26, 2016
1 parent 2a965a2 commit 6f86e53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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*

Expand Down
2 changes: 1 addition & 1 deletion packetbeat/beater/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
32 changes: 26 additions & 6 deletions packetbeat/sniffer/sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,44 @@ 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
}

ret := []string{}
for _, dev := range devices {
r := 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)
r += fmt.Sprintf(" (%s)", desc)
}

if withIP {
ips := "Not assigned ip address"
if len(dev.Addresses) > 0 {
ips = ""

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())
}
}
r += fmt.Sprintf(" (%s)", ips)

}
ret = append(ret, r)
}
return ret, nil
}
Expand All @@ -113,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)
}
Expand Down

0 comments on commit 6f86e53

Please sign in to comment.