new function: allow hostname to be set

This commit is contained in:
th33xitus
2020-07-18 18:33:55 +02:00
parent 1cc94d4874
commit e4cfad4162
2 changed files with 86 additions and 5 deletions

View File

@@ -254,4 +254,79 @@ read_octoprint_service_status(){
else
OPRINT_SERVICE_STATUS="${red}[Disable]${default} OctoPrint Service "
fi
}
create_custom_hostname(){
echo
top_border
echo -e "| You can change the hostname of this machine to use |"
echo -e "| that name to open the Interface in your browser. |"
echo -e "| This option is completely optional and can also be |"
echo -e "| done at a later point in time if you are unsure. |"
echo -e "| |"
echo -e "| Example: If you set the hostname to 'my-printer' |"
echo -e "| you can open Mainsail by browsing to: |"
echo -e "| http://my-printer.local |"
bottom_border
while true; do
echo -e "${cyan}"
read -p "###### Do you want to change the hostname? (Y/n): " yn
echo -e "${default}"
case "$yn" in
Y|y|Yes|yes|"") set_hostname; break;;
N|n|No|no) break;;
esac
done
}
set_hostname(){
#check for existing avahi installation
status_msg "Checking for necessary Avahi installation ..."
if ! [[ $(dpkg-query -f'${Status}' --show avahi-daemon 2>/dev/null) = *\ installed ]]; then
status_msg "Installing Avahi ..."
sudo apt-get install avahi-daemon -y && ok_msg "Avahi successfully installed!"
fi
ok_msg "Avahi found!"
#get current hostname and write to variable
HOSTNAME=$(hostname)
#create host file if missing or create backup of existing one with current date&time
if [ -f /etc/hosts ]; then
status_msg "Creating backup of hosts file ..."
get_date
sudo cp /etc/hosts /etc/hosts."$current_date".bak
ok_msg "Backup done!"
ok_msg "File:'/etc/hosts."$current_date".bak'"
else
sudo touch /etc/hosts
fi
echo
top_border
echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${default} |"
echo -e "| ${red}No special characters allowed!${default} |"
echo -e "| ${red}No leading or trailing '-' allowed!${default} |"
bottom_border
while true; do
echo -e "${cyan}"
read -p "###### Please set the new hostname: " NEW_HOSTNAME
echo -e "${default}"
if [[ $NEW_HOSTNAME =~ ^[^\-]+([0-9a-z]\-{0,1})+[^\-]+$ ]]; then
ok_msg "'$NEW_HOSTNAME' is a valid hostname!"
#set hostname in /etc/hostname
status_msg "Setting hostname to '$NEW_HOSTNAME' ..."
status_msg "Please wait ..."
sudo hostnamectl set-hostname $NEW_HOSTNAME
#write new hostname to /etc/hosts
status_msg "Writing new hostname to /etc/hosts ..."
if cat /etc/hosts | grep "###set by kiauh" &>/dev/null; then
sudo sed -i "/###set by kiauh/s/\<$HOSTNAME\>/$NEW_HOSTNAME/" /etc/hosts
else
echo "127.0.0.1 $NEW_HOSTNAME ###set by kiauh" | sudo tee -a /etc/hosts &>/dev/null
fi
ok_msg "New hostname successfully configured!"
ok_msg "You need to reboot your machine for changes to take effect!"
break
else
warn_msg "'$NEW_HOSTNAME' is not a valid hostname!"
fi
done
}