#!/bin/bash # vars VAR_LIST="HOSTNAME_MODE HOSTNAME_STRING HOSTNAME_IFACE LLXCFG_IFACE" LLX_GETVAR="$(which llxcfg-showvars)" LLX_NETFUNCS="/usr/share/lliurex/lliurex-netfuncs/lliurex-netfuncs.sh" HOSTNAME_PATH="/etc/hostname" HOSTS_PATH="/etc/hosts" DNS_TIMEOUT=2 # Usage: exit_message message [exit_code] exit_message() { local message="$1" local exit_code=${2:-0} if [ $exit_code -eq 0 ]; then echo -e "$message" else echo -e "$message" >&2 fi exit $exit_code } # Usage: set_hostname hostname set_hostname(){ local newhostname="$1" local curhostname="$(hostname)" if [ -z "$newhostname" ]; then return 1; fi echo $newhostname|skel-install "$HOSTNAME_PATH" sed "/^127.0.1.1[[:blank:]]/d" "$HOSTS_PATH"|skel-install "$HOSTS_PATH" hostname $newhostname } [ -x ${LLX_GETVAR} ] && eval `${LLX_GETVAR} ${VAR_LIST}` [ "$HOSTNAME_IFACE" ] || HOSTNAME_IFACE="$LLXCFG_IFACE" # some tests [ -f "$LLX_NETFUNCS" ] || exit 0 . "$LLX_NETFUNCS" NEW_HOSTNAME="" case $HOSTNAME_MODE in FIXED) NEW_HOSTNAME="$HOSTNAME_STRING" ;; DNS) if [ "$HOSTNAME_IFACE" ] ; then cur_ip=$(get_current_ip "$HOSTNAME_IFACE") if [ "$cur_ip" ] ; then res=0 NEW_HOSTNAME="$(dig -x $cur_ip +short +time=$DNS_TIMEOUT |sed -e "s%\.$%%")" || res=$? # if [ $res -eq 0 -a "$NEW_HOSTNAME" ]; then # # remove from dot # NEW_HOSTNAME="${NEW_HOSTNAME%%.*}" # fi fi fi ;; esac if [ "$NEW_HOSTNAME" ] ; then [ ! -r "$HOSTNAME_PATH" ] || curhostname=$(<$HOSTNAME_PATH) if [ "$NEW_HOSTNAME" != "$curhostname" ] ; then set_hostname "$NEW_HOSTNAME" fi fi exit 0