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.

131 lines
3.2 KiB
Go

3 years ago
package system
import (
"bytes"
"errors"
"fmt"
"hash/fnv"
"net"
"os/exec"
"strings"
3 years ago
)
// this package serves to add in wrappers for system commands to get hwinfo from the board
// this package does not actually use the hwinfo command, but rather gathers hardware info from a variety of commands
// command for model :
// lshw -C system 2>/dev/null | head -n 1
3 years ago
// command for ip && mac address :
// ifconfig eth0 | awk '/inet |ether / {print $2}'
3 years ago
// can combine and dump into file using
// lshw -C system 2>/dev/null | head -n 1 > hwinfo.txt && ifconfig eth0 | awk '/inet |ether / {print $2}' >> hwinfo.txt
// *** will just replace info in file everytime
3 years ago
func GetId(eth string) (int, error) {
maccmd := fmt.Sprintf("ifconfig %v | awk '/ether / {print $2}'", eth)
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(eth string) (string, error) {
ipcmd := fmt.Sprintf("ifconfig %v | awk '/inet / {print $2}'", eth)
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) {
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) {
bus := map[string]int{"raspberrypi": 1, "beaglebone": 2}
devname := "lshw -C system 2>/dev/null | head -n 1"
var stderr, out bytes.Buffer
cmd := exec.Command("bash", "-c", devname)
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return 0, err
}
b := out.String()
b = strings.Trim(b, " \n")
if bs, ok := bus[b]; !ok {
return 0, errors.New(fmt.Sprintf("No bus for dev %v", b))
} else {
return bs, nil
}
}
func GetModel() (string, error) {
devname := "lshw -C system 2>/dev/null | head -n 1"
var stderr, out bytes.Buffer
cmd := exec.Command("bash", "-c", devname)
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
3 years ago
ipcmd := "ifconfig eth0 | awk '/inet / {print $2}'"
maccmd := "ifconfig eth0 | awk '/ether / {print $2}'"
devcmd := "lshw -C system 2>/dev/null | head -n 1"
3 years ago
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")
3 years ago
hash := fnv.New32a()
hash.Write(res[1].Bytes())
3 years ago
b := res[2].String()
b = strings.Trim(b, " \n")
return nil
3 years ago
}