From 1d22c6dc076c8b8efd5a6fd4addc3cbc85182a37 Mon Sep 17 00:00:00 2001 From: Tim Eertmoed Date: Mon, 28 Oct 2024 14:02:25 +0100 Subject: [PATCH] =?UTF-8?q?usr/bin/hostname=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/bin/hostname | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 usr/bin/hostname diff --git a/usr/bin/hostname b/usr/bin/hostname new file mode 100644 index 0000000..768edcd --- /dev/null +++ b/usr/bin/hostname @@ -0,0 +1,32 @@ +#!/bin/bash + +LOGFILE="/var/log/hostname.log" + +# Function to read the hostname from the DHCP lease file +get_hostname() { + sudo cat /var/lib/dhcp/* | grep -a "option host-name" | tail -1 | \ + awk -F '"' '{print $2}' +} + +# Function to update /etc/hosts with the new hostname +update_hosts() { + local hostname="$1" + # Replace the second line with the new hostname + sudo sed -i "2s/.*/127.0.1.1 ${hostname}/" /etc/hosts + echo "Updated /etc/hosts with hostname: $hostname" >> $LOGFILE +} + +# Read the hostname from DHCP +hostname=$(get_hostname) + +if [ -n "$hostname" ]; then + update_hosts "$hostname" +else + echo "No hostname found." >> $LOGFILE +fi + +# Log completion +echo "Hostname update script completed." >> $LOGFILE + +# Version 1.0: +# Created 2024 by Tim Eertmoed, Christian Hampp @ WiS IT-Solutions GmbH, Germany to work on Raspian as custom pxe init script.