From 72c250bcaf4c8d24dbb5a812816540d75e83b105 Mon Sep 17 00:00:00 2001 From: Steffen Probst Date: Thu, 11 Nov 2021 12:34:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=80=9Eetc/rc.local=E2=80=9C=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update der /etc/hosts und hostname im. --- etc/rc.local | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/etc/rc.local b/etc/rc.local index 7d738cb..a8f0fb3 100755 --- a/etc/rc.local +++ b/etc/rc.local @@ -31,13 +31,37 @@ fi #ip a s wlan1 2&>1 /dev/null || #Check einbauen der aktive Verbindung prüft if [ -d /home/pi ]; then - echo pi$(cat /sys/class/net/wlan0/address | sed '/://g') > /tmp/hostname && hostname --file /tmp/hostname + echo pi$(cat /sys/class/net/wlan0/address | sed s'/://g') > /tmp/hostname && hostname --file /tmp/hostname else - echo thin$(cat /sys/class/net/wlan0/address | sed '/://g') > /tmp/hostname && hostname --file /tmp/hostname + echo thin$(cat /sys/class/net/wlan0/address | sed s'/://g') > /tmp/hostname && hostname --file /tmp/hostname +fi + +cp /tmp/hostname /etc/hostname + +# insert/update hosts entry +ip_address="127.0.2.1" +host_name=$(cat /tmp/hostname) +# find existing instances in the host file and save the line numbers +matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)" +host_entry="${ip_address} ${host_name} ${host_name}.lebenshilfe.local" + +echo "Please enter your password if requested." + +if [ ! -z "$matches_in_hosts" ] +then + echo "Updating existing hosts entry." + # iterate over the line numbers on which matches were found + while read -r line_number; do + # replace the text of each line with the desired host entry + sudo sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts + done <<< "$matches_in_hosts" +else + echo "Adding new hosts entry." + echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null fi #Fuer eth0, wenn kein WLAN vorhanden ist. Kann dann auskommentiert werden. -#echo rpi$(cat /sys/class/net/eth0/address | sed '/://g') > /tmp/hostname && hostname --file /tmp/hostname +#echo rpi$(cat /sys/class/net/eth0/address | sed s'/://g') > /tmp/hostname && hostname --file /tmp/hostname # set network