package system import ( "os/exec" "bytes" "hash/fnv" "strings" ) // 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 // command for ip && mac address : // ifconfig eth0 | awk '/inet |ether / {print $2}' // 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 type HWinfo struct { Ip string Id uint32 Bus int Model string Port int } func NewHWinfo() (*HWinfo, error) { h := &HWinfo{Port:2000} err := h.Get() return h, err } func (h *HWinfo) 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") h.Ip = ip hash := fnv.New32a() hash.Write(res[1].Bytes()) h.Id = hash.Sum32() b := res[2].String() b = strings.Trim(b," \n") h.Model = b h.Bus = bus[b] return nil }