stable #2
@@ -2,6 +2,11 @@
|
||||
|
||||
LOGFILE="/var/log/hostname.log"
|
||||
|
||||
# Function to log messages with timestamps
|
||||
log() {
|
||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOGFILE"
|
||||
}
|
||||
|
||||
# Function to read the hostname from the DHCP lease file
|
||||
get_hostname() {
|
||||
sudo cat /var/lib/dhcp/* | grep -a "option host-name" | tail -1 | \
|
||||
@@ -13,7 +18,7 @@ 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
|
||||
log "Updated /etc/hosts with hostname: $hostname"
|
||||
}
|
||||
|
||||
# Read the hostname from DHCP
|
||||
@@ -22,14 +27,14 @@ hostname=$(get_hostname)
|
||||
if [ -n "$hostname" ]; then
|
||||
# Set the hostname using hostnamectl
|
||||
sudo hostnamectl set-hostname "$hostname"
|
||||
echo "Set hostname to: $hostname" >> $LOGFILE
|
||||
log "Set hostname to: $hostname"
|
||||
update_hosts "$hostname"
|
||||
else
|
||||
echo "No hostname found." >> $LOGFILE
|
||||
log "No hostname found."
|
||||
fi
|
||||
|
||||
# Log completion
|
||||
echo "Hostname update script completed." >> $LOGFILE
|
||||
log "Hostname update script completed."
|
||||
|
||||
# Version 1.0:
|
||||
# Created 2024 by Tim Eertmoed, Christian Hampp @ WiS IT-Solutions GmbH, Germany to work on Raspian as custom pxe init script.
|
||||
# Created 2024 by Tim Eertmoed, Christian Hampp @ WiS IT-Solutions GmbH, Germany to work on Raspbian as custom PXE init script.
|
||||
|
||||
Reference in New Issue
Block a user