13

I would like to identify which RaspberryPI hardware instance is booting on my network.

I have plans for using a LOT (not just 10) of them as clients in a larger solution, but I would like to reuse the SD-image for them all.

Is there any kinda way to identify them from each other, unless I place somekinda serial code in a file somewhere in the OS?

Btw. I use MONO/C# for programming, but I am just curious to know if the PI has a hardware serialnumber somehow or not?

BerggreenDK
  • 364
  • 1
  • 3
  • 14

3 Answers3

14

You can use the same number used for buying the MPEG license.

Quoting MPEG-2 license key:

To find your serial number, type cat /proc/cpuinfo at the command line as shown below:

pi@raspberrypi:~$ cat /proc/cpuinfo

Processor       : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS        : 697.95
Features        : swp half thumb fastmult vfp edsp java tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xb76
CPU revision    : 7
Hardware        : BCM2708
Revision        : 1000002
Serial          : 000000000000000d

Serial is what you're looking for.

techraf
  • 4,319
  • 10
  • 31
  • 42
Remo.D
  • 534
  • 5
  • 11
  • Ahh, nice idea. Do you have any clue of how to retrieve this from Mono? – BerggreenDK Feb 28 '13 at 09:25
  • Only problem is that the client/user will have to buy the MPEG license just for that purpose alone. – BerggreenDK Feb 28 '13 at 09:26
  • @BerggreenDK. Not sure what you mean. THe serial number that appears with the cpuinfo command is the serial number of the CPU. It's there whether you have an MPEG licence or not. – Remo.D Feb 28 '13 at 09:49
  • your serial number is 0...d? – Piotr Kula Feb 28 '13 at 09:58
  • 1
    No, the '00000...d' is just there as an example. Try doing it on your raspberry and you'll get yours. – Remo.D Feb 28 '13 at 10:00
  • @BerggreenDK. To get it from Mono you just open the file (as any regular text file), read from it and extract the serial code. – Remo.D Feb 28 '13 at 10:11
  • Ooohhh... now thats rather cool too. But if its filebased, isnt it "changeable" or "cloneable"? – BerggreenDK Feb 28 '13 at 12:46
  • I tried the cat /proc/cpuinfo on a virtual box I have Debian running on too. Intel Core2 Quad Q9300 @2.50GHz - but there is no serial on that? Thought it was a Debian thing. – BerggreenDK Feb 28 '13 at 12:49
  • 1
    It is an hardware code that is unique for any Raspberry (I have two of them), it is not file based. I would have been surprised if you had it from Virtual Box. – Remo.D Feb 28 '13 at 13:20
  • So cloning the SDcard wouldnt clone the cpuID I conclude logically, right? – BerggreenDK Feb 28 '13 at 14:04
  • Decided to mark your answer as THE answer, @Remo.D as its more "secure", but I am going to keep the MAC idea as fall-over/fall-back for when the same client program is running on other hardware types. – BerggreenDK Feb 28 '13 at 14:06
6

You could use the MAC address of the ethernet adaptor; use a statement something like this to extract it into a variable:

mysn=`ifconfig eth0 | grep HWaddr |sed -e's/.*HWaddr\s\(\S*\).*/\1/'`
Jacobm001
  • 11,898
  • 7
  • 46
  • 56
TomG
  • 1,171
  • 2
  • 10
  • 22
  • A nice suggestion. If I could only see how I should retreive this from MONO. But the idea is great. Had completely forgot to think about the onboard LAN. MAC numbers are enough indeed. Just need to figure out how to pull that information from the System.Net library. – BerggreenDK Feb 28 '13 at 09:27
  • Found this: http://stackoverflow.com/questions/850650/reliable-method-to-get-machines-mac-address-in-c-sharp – BerggreenDK Feb 28 '13 at 09:30
  • 1
    Note that you can modify the MAC address of the raspberry. This is a viable solution only if you don't need to be absolutely sure about the identity of your Raspberry Pi. The CPU serial code obtainable with cpuinfo is not alterable. – Remo.D Feb 28 '13 at 09:56
  • okay, thats a great point too. Yes, MAC can be changed on some LAN hardware. Thats true. But I am merly concerned that someone would clone my SDCARD and then use it for multiple clients without my knowledge. That would be bad for my architecture of the solution. – BerggreenDK Feb 28 '13 at 12:51
  • Sorry @TomG I really appricated your idea and I am going to use it as "fall back" for detecting devices that arent Raspberry PI hardware as my client-programs will be crossplatform. I wish I could mark two answers in this case. – BerggreenDK Feb 28 '13 at 14:05
4

In C# Mono I'm doing this:

private string GetRpiSerial()
{
    CommandSync("cat /proc/cpuinfo | grep Serial | cut -d ':' -f 2 > /home/pi/serial.txt");

    using (var sr = new StreamReader("/home/pi/serial.txt"))
    {
        return sr.ReadLine().Trim();
    }
}

public static void CommandSync(string cmd, string args)
{
    var info = new ProcessStartInfo();
    info.FileName = "/bin/bash";
    info.Arguments = string.Format("-c \"sudo {0} {1}\"", cmd, args);
    info.UseShellExecute = false;
    info.RedirectStandardOutput = true;

    var p = Process.Start(info);
    p.WaitForExit();
}

And retrieve anywhere with:

var rpiSerial = GetRpiSerial();
Jacobm001
  • 11,898
  • 7
  • 46
  • 56
Tico Fortes
  • 156
  • 2
  • This answer is even better than Remo's original answer, as it includes the solution in C# as I originally asked. Very nice of you to produce this one for us all. – BerggreenDK Apr 16 '16 at 01:25