Add auto hostname setting

This commit is contained in:
Donald Burr 2015-02-17 22:41:20 -08:00
parent 4b799bb44b
commit ca552ae331
3 changed files with 18 additions and 18 deletions

View file

@ -3,10 +3,11 @@ NUM=0
while read FOO; do while read FOO; do
if ! echo $FOO | grep -q ^#; then if ! echo $FOO | grep -q ^#; then
HOST="`echo $FOO | cut -d\| -f3`" HOST="`echo $FOO | cut -d\| -f3`"
MAC="`echo $FOO | cut -d\| -f1 | tr '[A-Z]' '[a-z]'`"
IP="`echo $FOO | cut -d\| -f2`" IP="`echo $FOO | cut -d\| -f2`"
if ! echo $HOST | egrep -q "(server|spare)"; then if ! echo $HOST | egrep -q "(server|spare)"; then
>&2 echo found host $HOST ip $IP num $NUM >&2 echo found host $HOST mac $MAC ip $IP num $NUM
echo "$HOST:$IP:$NUM" echo "$HOST|$MAC|$IP|$NUM"
NUM=$((NUM+1)) NUM=$((NUM+1))
elif echo $HOST | grep -q server; then elif echo $HOST | grep -q server; then
>&2 echo found server at ip $IP >&2 echo found server at ip $IP

View file

@ -1,17 +1,16 @@
#!/bin/bash #!/bin/bash
if grep -q raspberrypi /etc/hostname; then MAC=`ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | tr '[A-Z]' '[a-z]'`
echo "You still have the default hostname set. We recommend changing the" if grep -q $MAC HOSTS; then
echo "hostname so that your Pi can be uniquely identified." HOSTNAME=`grep $MAC HOSTS | cut -d\| -f1`
echo "If you wish to change it, enter a new hostname now, otherwise" echo "setting hostname to $HOSTNAME"
echo -n "just press Return: " echo "$HOSTNAME" > /tmp/HOSTNAME.$$
read NEW_HOSTNAME sudo rm -f /etc/hostname
if [ ! -z "$NEW_HOSTNAME" ]; then sudo mv /tmp/HOSTNAME.$$ /etc/hostname
echo "$NEW_HOSTNAME" > /tmp/HOSTNAME.$$ sudo chown root:root /etc/hostname
sudo rm -f /etc/hostname sudo chmod 644 /etc/hostname
sudo mv /tmp/HOSTNAME.$$ /etc/hostname sudo /etc/init.d/hostname.sh
sudo chown root:root /etc/hostname else
sudo chmod 644 /etc/hostname echo "cannot determine hostname"
sudo /etc/init.d/hostname.sh fi
fi exit 1
fi

View file

@ -2,7 +2,7 @@
SERVERIP=`cat SERVERIP` SERVERIP=`cat SERVERIP`
HOSTNAME=`hostname` HOSTNAME=`hostname`
NUM=`grep ^$HOSTNAME: HOSTS | cut -d: -f3` NUM=`grep ^$HOSTNAME\| HOSTS | cut -d\| -f4`
if [ -z "$NUM" ]; then if [ -z "$NUM" ]; then
echo "error: could not find host number for `hostname`!" echo "error: could not find host number for `hostname`!"
exit 1 exit 1