From ca552ae331fab0d7234c26789c6923bf96bcc23c Mon Sep 17 00:00:00 2001 From: Donald Burr Date: Tue, 17 Feb 2015 22:41:20 -0800 Subject: [PATCH] Add auto hostname setting --- acatcher/mkhosts | 5 +++-- acatcher/set_hostname.sh | 29 ++++++++++++++--------------- acatcher/stream.sh | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/acatcher/mkhosts b/acatcher/mkhosts index abd4795..92805ed 100755 --- a/acatcher/mkhosts +++ b/acatcher/mkhosts @@ -3,10 +3,11 @@ NUM=0 while read FOO; do if ! echo $FOO | grep -q ^#; then HOST="`echo $FOO | cut -d\| -f3`" + MAC="`echo $FOO | cut -d\| -f1 | tr '[A-Z]' '[a-z]'`" IP="`echo $FOO | cut -d\| -f2`" if ! echo $HOST | egrep -q "(server|spare)"; then - >&2 echo found host $HOST ip $IP num $NUM - echo "$HOST:$IP:$NUM" + >&2 echo found host $HOST mac $MAC ip $IP num $NUM + echo "$HOST|$MAC|$IP|$NUM" NUM=$((NUM+1)) elif echo $HOST | grep -q server; then >&2 echo found server at ip $IP diff --git a/acatcher/set_hostname.sh b/acatcher/set_hostname.sh index 893103a..01082c6 100755 --- a/acatcher/set_hostname.sh +++ b/acatcher/set_hostname.sh @@ -1,17 +1,16 @@ #!/bin/bash - if grep -q raspberrypi /etc/hostname; then - echo "You still have the default hostname set. We recommend changing the" - echo "hostname so that your Pi can be uniquely identified." - echo "If you wish to change it, enter a new hostname now, otherwise" - echo -n "just press Return: " - read NEW_HOSTNAME - if [ ! -z "$NEW_HOSTNAME" ]; then - echo "$NEW_HOSTNAME" > /tmp/HOSTNAME.$$ - sudo rm -f /etc/hostname - sudo mv /tmp/HOSTNAME.$$ /etc/hostname - sudo chown root:root /etc/hostname - sudo chmod 644 /etc/hostname - sudo /etc/init.d/hostname.sh - fi - fi +MAC=`ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | tr '[A-Z]' '[a-z]'` +if grep -q $MAC HOSTS; then + HOSTNAME=`grep $MAC HOSTS | cut -d\| -f1` + echo "setting hostname to $HOSTNAME" + echo "$HOSTNAME" > /tmp/HOSTNAME.$$ + sudo rm -f /etc/hostname + sudo mv /tmp/HOSTNAME.$$ /etc/hostname + sudo chown root:root /etc/hostname + sudo chmod 644 /etc/hostname + sudo /etc/init.d/hostname.sh +else + echo "cannot determine hostname" +fi +exit 1 diff --git a/acatcher/stream.sh b/acatcher/stream.sh index 43c7c09..5e80e8d 100755 --- a/acatcher/stream.sh +++ b/acatcher/stream.sh @@ -2,7 +2,7 @@ SERVERIP=`cat SERVERIP` HOSTNAME=`hostname` -NUM=`grep ^$HOSTNAME: HOSTS | cut -d: -f3` +NUM=`grep ^$HOSTNAME\| HOSTS | cut -d\| -f4` if [ -z "$NUM" ]; then echo "error: could not find host number for `hostname`!" exit 1