Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IP address to -devices command output #2327

Merged
merged 4 commits into from
Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*

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