You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
2.6 KiB
Go
119 lines
2.6 KiB
Go
// package system uses linux commands to get hardware info from devices
|
|
package system
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"net"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func GetId() (int, error) {
|
|
// gets the mac address and hashes into consistent id
|
|
maccmd := fmt.Sprintf("ifconfig %v | awk '/ether / {print $2}'", et)
|
|
var stderr bytes.Buffer
|
|
var out bytes.Buffer
|
|
cmd := exec.Command("bash", "-c", maccmd)
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &stderr
|
|
if err := cmd.Run(); err != nil {
|
|
return 0, err
|
|
}
|
|
hash := fnv.New32a()
|
|
hash.Write(out.Bytes())
|
|
id := hash.Sum32()
|
|
return int(id), nil
|
|
}
|
|
|
|
func GetIp() (string, error) {
|
|
ipcmd := "ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q'"
|
|
var stderr bytes.Buffer
|
|
var out bytes.Buffer
|
|
cmd := exec.Command("bash", "-c", ipcmd)
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &stderr
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
ip := strings.Trim(out.String(), " \n")
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
func GetPort() (int, error) {
|
|
// obsolete
|
|
if addr, err := net.ResolveTCPAddr("tcp", ":0"); err != nil {
|
|
return 0, err
|
|
} else if lis, err := net.ListenTCP("tcp", addr); err != nil {
|
|
return 0, err
|
|
} else {
|
|
defer lis.Close()
|
|
return lis.Addr().(*net.TCPAddr).Port, nil
|
|
}
|
|
}
|
|
|
|
func GetBus() (int, error) {
|
|
// preset busses
|
|
busList := map[string]int{"raspberrypi": 1, "beaglebone": 2}
|
|
// vars
|
|
var bus int
|
|
var ok bool
|
|
|
|
if name, err =: GetModel(); err != nil {
|
|
return bus, err
|
|
} else if bus, ok = busList[b]; !ok {
|
|
return 0, errors.New(fmt.Sprintf("No bus for dev %s", b))
|
|
}
|
|
|
|
// returns correct bus
|
|
return bus, nil
|
|
}
|
|
|
|
func GetModel() (string, error) {
|
|
var stderr, out bytes.Buffer
|
|
cmd := exec.Command("bash", "-c", "hostname")
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &stderr
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
b := out.String()
|
|
b = strings.Trim(b, " \n")
|
|
return b, nil
|
|
}
|
|
|
|
func Get() error {
|
|
// responsible for filling out struct
|
|
//bus := map[string]int{"raspberrypi":1,"beaglebone":2} // eventually will replace this with a config file
|
|
|
|
ipcmd := "ifconfig eth0 | awk '/inet / {print $2}'"
|
|
maccmd := "ifconfig eth0 | awk '/ether / {print $2}'"
|
|
devcmd := "lshw -C system 2>/dev/null | head -n 1"
|
|
|
|
res := [3]bytes.Buffer{}
|
|
var stderr bytes.Buffer
|
|
cmds := []string{ipcmd, maccmd, devcmd}
|
|
for i, c := range cmds {
|
|
cmd := exec.Command("bash", "-c", c)
|
|
cmd.Stdout = &res[i]
|
|
cmd.Stderr = &stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// formatting
|
|
ip := res[0].String()
|
|
ip = strings.Trim(ip, " \n")
|
|
|
|
hash := fnv.New32a()
|
|
hash.Write(res[1].Bytes())
|
|
|
|
b := res[2].String()
|
|
b = strings.Trim(b, " \n")
|
|
return nil
|
|
}
|