Files
RPS-Client/usr/bin/hostname

33 lines
880 B
Bash

#!/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.