Hi,
I have two Raspberry Pi and both work with same custom image file. I needed a way to differentiate them when I overwrote a SD Card (dd cloned image). This following script set the host name from the hardware serial (like rpi-bf8).
Script is scheduled with the @reboot flag in crontab.
Unique hostname is important for my because I use this to remote connection (ssh rpi-bf8.b.net ... dnsmasq dynamique dns)
I don't know is this is the good way (?) and if is helpful for someone but it's working fine for me.
$this->bbcode_second_pass_code('', '
#!/usr/bin/python
from subprocess import call
hostNameFilePath = '/etc/hostname'
currentHostName = file(hostNameFilePath).read().strip()
serial = file('/proc/cpuinfo').read().split('Serial')[1].strip()[-3:] # 3 mean last 3 character of the serial
newHostName = 'rpi-'+serial
if newHostName != currentHostName:
file(hostNameFilePath, 'w').write(newHostName + '\n')
call('/sbin/reboot')
')
Bye