Getting Your Raspberry Pi Serial Number Using Python
26 April 2015
Solution
Bash/Perl
In Bash, it is very simple to extract… by using Perl. Use
cat /proc/cpuinfo | perl -n -e '/^Serial[ ]*: ([0-9a-f]{16})$/ && print "$1\n"'
For example,
$ cat cpuinfo | perl -n -e '/^Serial[ ]*: ([0-9a-f]{16})$/ && print "$1\n"'
000000000000000d
Python
Raspberry Spy provide a very useful Python example.
def getserial():
# Extract serial from cpuinfo file
cpuserial = "0000000000000000"
try:
f = open('/proc/cpuinfo','r')
for line in f:
if line[0:6]=='Serial':
cpuserial = line[10:26]
f.close()
except:
cpuserial = "ERROR000000000"
return cpuserial
References
Raspberry Spy: Getting Your Raspberry Pi Serial Number Using Python
blog comments powered by Disqus