2016-07-20 20:13:50 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// linkSpeed returns link speed in Mb/s, or 0 when unable to determine it.
|
|
|
|
func (f *NetworkFingerprint) linkSpeed(device string) int {
|
|
|
|
command := fmt.Sprintf("Get-NetAdapter -IncludeHidden | Where name -eq '%s' | Select -ExpandProperty LinkSpeed", device)
|
|
|
|
path := "powershell.exe"
|
|
|
|
outBytes, err := exec.Command(path, command).Output()
|
|
|
|
|
|
|
|
if err != nil {
|
2018-10-04 23:41:40 +00:00
|
|
|
f.logger.Warn("failed to detect link speed", "path", path, "command", command, "error", err)
|
2016-07-20 20:13:50 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
output := strings.TrimSpace(string(outBytes))
|
2016-07-22 20:41:36 +00:00
|
|
|
|
|
|
|
return f.parseLinkSpeed(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *NetworkFingerprint) parseLinkSpeed(commandOutput string) int {
|
|
|
|
args := strings.Split(commandOutput, " ")
|
2016-07-20 20:13:50 +00:00
|
|
|
if len(args) != 2 {
|
2018-10-04 23:41:40 +00:00
|
|
|
f.logger.Warn("couldn't split LinkSpeed output", "output", commandOutput)
|
2016-07-20 20:13:50 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
unit := strings.Replace(args[1], "\r\n", "", -1)
|
|
|
|
value, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
2018-10-04 23:41:40 +00:00
|
|
|
f.logger.Warn("unable to parse LinkSpeed value", "value", commandOutput)
|
2016-07-20 20:13:50 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
switch unit {
|
|
|
|
case "Mbps":
|
|
|
|
return value
|
|
|
|
case "Kbps":
|
|
|
|
return value / 1000
|
|
|
|
case "Gbps":
|
|
|
|
return value * 1000
|
|
|
|
case "bps":
|
|
|
|
return value / 1000000
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|