Getting hardware information in Go

7,906 views
Skip to first unread message

Artemis

unread,
May 19, 2014, 7:13:52 AM5/19/14
to golan...@googlegroups.com
Hey!

I'm looking for a way to get some hardware information, such as CPU model, disk serial number, etc. Currently, the only way I found to get some information is through enumerating the network interfaces to get their MAC addresses. Unfortunately, this is not enough for me to be usable as a unique identifier. I haven't tried looking up C possibilities through Cgo, since I'm not a C programmer.

Thanks for your help!

Jan Mercl

unread,
May 19, 2014, 7:17:49 AM5/19/14
to Artemis, golang-nuts
Are you after this because of a DRM/licensing scheme?

-j

Archos

unread,
May 19, 2014, 7:29:46 AM5/19/14
to golan...@googlegroups.com
The better unique identifer that you can get is the BIOS, so access to the raw device and read the first bytes (512 bytes?). This is easier in Unix systems, but I'm not sure about Windows.

Konstantin Khomoutov

unread,
May 19, 2014, 8:15:42 AM5/19/14
to Artemis, golan...@googlegroups.com
The problem is not C as C is no magic. The problem is in particular
ways a particular OS (or even kernel) makes this information available.
For instance, under Linux you're supposed to parse relevant bits
of /proc or /sys; on Windows you're supposed to call out to WMI etc.
C is not concerned with this as Go calls the OS's kernel it's running
on directly, using syscalls, and this layer is exposed to the
programmer using the syscall and unsafe packages (the latter is not
strictly needed for syscalls but helps gatewaying data between Go and
the kernel).

So to recap, discover which ways to get H/W info your target platform
does then try to use it via the syscall package (look at the
src/pkg/syscall directory in the Go sources for examples) and then come
back if/when you have problems with this.

Donovan Hide

unread,
May 19, 2014, 8:21:55 AM5/19/14
to Konstantin Khomoutov, Artemis, golang-nuts
On OS X, a cheeky hack could be to just save the output of this with os/exec:

system_profiler SPHardwareDataType | sha512sum

Similarly with linux:

dmidecode | sha512sum

Untested :-)

Donovan Hide

unread,
May 19, 2014, 8:30:51 AM5/19/14
to Konstantin Khomoutov, Artemis, golang-nuts
Oops, homebrew installed coreutils has different command names. Vanilla OS X solution:

system_profiler SPHardwareDataType | shasum -a 512 


Michael Jones

unread,
May 19, 2014, 8:44:24 AM5/19/14
to Donovan Hide, Konstantin Khomoutov, Artemis, golang-nuts
On Linux, you might like: https://github.com/c9s/goprocinfo


On Mon, May 19, 2014 at 5:30 AM, Donovan Hide <donov...@gmail.com> wrote:
Oops, homebrew installed coreutils has different command names. Vanilla OS X solution:

system_profiler SPHardwareDataType | shasum -a 512 


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Michael T. Jones | Chief Technology Advocate  | m...@google.com |  +1 650-335-5765

Artemis

unread,
May 19, 2014, 4:31:49 PM5/19/14
to golan...@googlegroups.com
Thanks for your answers!
I suspected that I needed some system-specific code rather than having portable code. I'll be working on this, and maybe I will be releasing some kind of library if my work achieves a good quality.

Thanks again!

sagar....@gmail.com

unread,
Nov 5, 2015, 12:55:41 PM11/5/15
to golang-nuts
Hi,
Could you please send me sample code on how to get BIOS serial number? which works with Linux,Windows,MAC OS X only with GO language implementation.
I would be very grateful if you can send a sample code on how to do, thank you very much.


Thanks & Regards,
Sagar P

Matt Harden

unread,
Nov 5, 2015, 1:27:33 PM11/5/15
to sagar....@gmail.com, golang-nuts
I found this project for you: https://github.com/ochapman/godmi

It appears to work only on systems with /dev/mem, which I believe includes Linux and possibly OSX but not Windows. Also it has to run as root to access /dev/mem.

--

Konstantin Khomoutov

unread,
Nov 5, 2015, 2:02:11 PM11/5/15
to Matt Harden, sagar....@gmail.com, golang-nuts
On Thu, 05 Nov 2015 18:26:51 +0000
Matt Harden <matt....@gmail.com> wrote:

> I found this project for you: https://github.com/ochapman/godmi
>
> It appears to work only on systems with /dev/mem, which I believe
> includes Linux and possibly OSX but not Windows. Also it has to run
> as root to access /dev/mem.

On Windows, one might execute

wmic bios get serialnumber

and parse its output.
This program requires administrative rights there as well.

Another approach would be to work with that WMI layer through COM (via
go-ole for instance). Oh, and googling reveals someone already did the
hard part of this work (using go-ole): [1].

1. https://github.com/StackExchange/wmi/

sagar....@gmail.com

unread,
Nov 7, 2015, 4:22:57 AM11/7/15
to golang-nuts, sagar....@gmail.com
Thanks,
I executed the code in Linux for getting BIOS information......But the output is NIL.
Could you please help me,how to get BIOS serial number.
==================================================================================
package main

import (
"fmt"
)
func main(){
BS:=godmi.GetBIOSInformation()
fmt.Println("BS:",BS)
sys:=godmi.GetSystemInformation()
fmt.Println("sys:",sys)
BB:=godmi.GetBaseboardInformation()
fmt.Println("BB:",BB)
}
O/P:-
BS: <nil>
sys: <nil>
BB: <nil>

sagar....@gmail.com

unread,
Nov 8, 2015, 5:16:40 AM11/8/15
to golang-nuts, matt....@gmail.com, sagar....@gmail.com, Konstantin Khomoutov
Thanks,
I executed the code in Linux for getting BIOS information......But the output is NIL.
Could you please help me,how to get BIOS serial number.
==================================================================================
package main

import (
"fmt"
)
func main(){
BS:=godmi.GetBIOSInformation()
fmt.Println("BS:",BS)
sys:=godmi.GetSystemInformation()
fmt.Println("sys:",sys)
BB:=godmi.GetBaseboardInformation()
fmt.Println("BB:",BB)
}
O/P:-
BS: <nil>
sys: <nil>
BB: <nil>

Konstantin Khomoutov

unread,
Nov 8, 2015, 9:46:04 AM11/8/15
to golang-nuts, sagar....@gmail.com
On Saturday, November 7, 2015 at 12:22:57 PM UTC+3, sagar....@gmail.com wrote:
Thanks,
I executed the code in Linux for getting BIOS information......But the output is NIL.
Could you please help me,how to get BIOS serial number.
[...]
Did you run your compiled program as root?
Accessing this kind of information requires super-user privileges.

This package provides its own command-line utility as well,
so you might

    cd $GOPATH/src/github.com/ochapman/godmi/cmd

and then

    go build

to build that program.

You might also consider installing and using the native Linux `dmidecde` program
and see if it runs and outputs information.
Consider your system's manual on how to figure out which package
provides this program and how to instal it.
 

Konstantin Khomoutov

unread,
Nov 8, 2015, 10:02:17 AM11/8/15
to golang-nuts, sagar....@gmail.com
On Saturday, November 7, 2015 at 12:22:57 PM UTC+3, sagar....@gmail.com wrote:
Thanks,
I executed the code in Linux for getting BIOS information......But the output is NIL.
Could you please help me,how to get BIOS serial number.

OK, it seems that you first need to call the Init() function
provided by this package before accessing the data.

Still, on the system I tried the code on, it panics even when used correctly --
the data accessing functions are called after Init() finished.

sagar....@gmail.com

unread,
Nov 9, 2015, 5:27:27 AM11/9/15
to golang-nuts, sagar....@gmail.com, Konstantin Khomoutov
Thank you very much.
Reply all
Reply to author
Forward
0 new messages