Automatic hostname from hardware serial number

Guides written by the community, for the community, and only guides!

Automatic hostname from hardware serial number

Postby Fuson51 » Sun Jan 06, 2013 12:06 pm

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
Fuson51
 
Posts: 12
Joined: Sat Jan 05, 2013 7:32 pm

Return to Community Guides

Who is online

Users browsing this forum: No registered users and 9 guests