Created
November 11, 2012 15:58
-
-
Save dweeber/4055331 to your computer and use it in GitHub Desktop.
Set RPI hostname based on /boot/serial_hostname.txt file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
### BEGIN INIT INFO | |
# Provides: serial_hostname | |
# Required-Start: $local_fs | |
# Required-Stop: | |
# X-Start-Before hostname | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Set hostname based on /boot/serial_hostname.txt | |
# Description: Read the machines hostname from /boot/serial_hostname.txt, | |
# based on machine serial number | |
### END INIT INFO | |
. /lib/lsb/init-functions | |
log_daemon_msg "Starting serial_hostname" | |
if [ -f /boot/serial_hostname.txt ] | |
then | |
SERIAL=`cat /proc/cpuinfo | grep Serial | awk '{ print $3 }'` | |
NEWHOST=`grep $SERIAL /boot/serial_hostname.txt | awk '{ print $2 }'` | |
if [ -z "$NEWHOST" ]; then | |
sed -i "$ a${SERIAL} ${SERIAL}" /boot/serial_hostname.txt | |
NEWHOST=$SERIAL | |
fi | |
if [ ! -z "$NEWHOST" ]; then | |
OLDHOST=`cat /etc/hostname` | |
log_daemon_msg "Changing $OLDHOST to $NEWHOST..." | |
echo $NEWHOST > /etc/hostname | |
sed -i "s/127.0.1.1.*/127.0.1.1\t$NEWHOST/g" /etc/hosts | |
fi | |
fi | |
log_daemon_msg "serial_hostname complete" | |
exit 0 |
Moved log_daemon_msg complete message outside of if statements.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original is a completed script from simplesi on the raspberry-pi forum. Format for serial_hostname.txt is:
000000004ca???00 Raspi01 Web
000000005ac???a1 Raspi02 Dev
0000000083b7???8 Raspi03 TV
Only the 2nd field is used (The 3rd is for a simple desc of the purpose of the RPi).