[SCRIPTS] Customization of Ubuntu - "Fog Service" for Ubuntu


  • Moderator

    Hi,

    I made two scripts to customize a master and enabled a “Fog Service” in ubuntu.
    I used it in dual boot configuration (Win7/Ubuntu 14.04)

    The first script - Cutom_My_Master (only launch on the master to prepare image) :

    • Update host (apt-get update / upgrade)
    • Modify Grub : Set timeout to 30s and default boot
    • Modify Gnome authentication : Disable login history
    • Enable Fog Service : add a service that allow to modify hostname, domain at startup/shutdown
    • Display a cool Hello Kitty at end (It was to justify my daughter why I was working at home)

    The second script - host_rename_fog (it’s the “FOG client”) :

    • It’s installed as a service
    • Update hostname and domain by contacting Fog Mysql Server (at startup/shutdown)
      -> Detect all networks cards and check if a mac address is present in Fog. If the hostname or domain is different it apply the update
      ->Turn all interfaces in DHCP in /etc/network/interface
      -> Updating /etc/hosts and /etc/hostname
      -> Remove persistent cards, very usefull in deployment
      -> Automatically restart machine to apply modification, and modify default boot to ubuntu once time.

    Howto :
    1/ Copy paste the code below in two files (saved in the same folder) : Custom_My_Master (in my case) and rename_host_fog (the name is important for this file, it’s used to declare the script in the init process).
    2/ Change right of Custom_My_Master : chmod 755 Custom_My_Master
    3/ Modify the rename_host_fog to match with your fog installation
    4/ Run Custom_My_Master : ./Custom_My_Master

    Scripts :

    Custom_My_Master

    #!/bin/bash
    #########################################################
    # Configure Ubuntu in Dual Boot
    #########################################################
    # Ch3i- 02/19/2015
    #########################################################
    
    #########################################################
    # Functions
    #########################################################
    
    FUNC_END(){
    echo -e "\033[45m##########################################################################
    #                          Updates Finished                              #
    ##########################################################################"
    cat <<"EOT"
                                                                              
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`   `4!!!!!!!!!!~4!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   <~:   ~!!!~   ..  4!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  ~~~~~~~     ud$$$$$  !!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  ~~~~~~~~~: ?$$$$$$$$$  !!!!!!!!!!!!!!
    !!!!!!!!!!!`     ``~!!!!!!!!!!!!!!  ~~~~~           *$$$$$k `!!!!!!!!!!!!!
    !!!!!!!!!!  $$$$$bu.   ~!~`     .   ~~~~      :~~~~          `4!!!!!!!!!!!
    !!!!!!!!!  $$$$$$$$$$$c  .zW$$$$$E ~~~~      ~~~~~~~~  ~~~~~:   !!!!!!!!!!
    !!!!!!!!! d$$$$$$$$$$$$$$$$$$$$$$E ~~~~~     ~~~~~~~~    ~~~~~  !!!!!!!!!!
    !!!!!!!!> 9$$$$$$$$$$$$$$$$$$$$$$$  ~~~~~~~  ~~~~~~~~     ~~~~  !!!!!!!!!!
    !!!!!!!!> $$$$$$$$$$$$$$$$$$$$$$$$b   ~~~     ~~~~~~~      ~~~  !!!!!!!!!!
    !!!!!!!!> $$$$$$$$$$$$$$$$$$$$$$$$$$$cuuue$$N.   ~        ~~~  !!!!!!!!!!!
    !!!!!!!!! **$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Ne  ~~~~~~~~  `!!!!!!!!!!!
    !!!!!!!!!  J$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$N  ~~~~~  zL  !!!!!!!!!!
    !!!!!!!!  d$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$c     z$$$c `!!!!!!!!!
    !!!!!!!> <$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$> 4!!!!!!!!
    !!!!!!!  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  !!!!!!!!
    !!!!!!! <$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*    ....:!!
    !!!!!!~ 9$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$e@$N  !!!!!!!
    !!!!!!  9$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  !!!!!!!
    !!!!!!  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  $$$$$$$$$$$~ ~~4!!!!
    !!!!!!  9$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    $$$$$$$Lue  :::!!!!
    !!!!!!> 9$$$$$$$$$$$$   $$$$$$$$$$$$$$$$$$$$$$$$$$$    $$$$$$$$$$  !!!!!!!
    !!!!!!!  $$*$$$$$$$$E    $$$$$$$$$$$$$$$$$$$$$$$$$$$u.@$$$$$$$$$E  !!!!!!!
    !!!!~`   .eeW$$$$$$$$   :$$$$$$$$$$$$$***$$$$$$$$$$$$$$$$$$$$u.    `~!!!!!
    !!> .:!h  $$$$$$$$$$$$ed$$$$$$$$$$$$Fz$$b $$$$$$$$$$$$$$$$$$$$$F  !h.  !!!
    !!!!!!!!L  $**$$$$$$$$$$$$$$$$$$$$$$ *$$$ $$$$$$$$$$$$$$$$$$$$F  !!!!!!!!!
    !!!!!!!!!   d$$$$$$$$$$$$$$$$$$$$$$$$buud$$$$$$$$$$$$$$$$$$$$   !!!!!!!!!!
    !!!!!!! .<!  #$$* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*  :!!!!!!!!!!!
    !!!!!!!!!!!!:   d$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#  :!!!!!!!!!!!!!
    !!!!!!!!!!!~  :   #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*     !!!!!!!!!!!!!!!
    !!!!!!!!!!  !!!!!:   ^ **$$$$$$$$$$$$$$$$$$$$**#      .:<!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!:...                      .::!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                                                                              
    EOT
    echo -e "\033[0m\n\n"
    }
    
    FUNC_TOP(){
    echo -e "\033[42m#########################################################
    #         Custom My Master - New master deploy          #
    #########################################################\033[0m\n\n"
    
    if [ "$STATUS_UPDATE_UPDATE" == "1" ]
    then
    	echo -n "Update in progress...";
    fi
    if [ "$STATUS_UPDATE_UPDATE" == "2" ]
    then
    	echo "Update in progress... Done";
    fi
    
    if [ "$STATUS_UPDATE_GRUB" == "1" ]
    then
    	echo -n "Grub configuration in progress...";
    fi
    if [ "$STATUS_UPDATE_GRUB" == "2" ]
    then
    	echo "Grub configuration in progress... Done";
    fi
    
    if [ "$STATUS_UPDATE_GNOME" == "1" ]
    then
    	echo -n "Gnome configuration in progress...";
    fi
    if [ "$STATUS_UPDATE_GNOME" == "2" ]
    then
    	echo "Gnome configuration in progress... Done";
    fi
    
    if [ "$STATUS_UPDATE_FOG" == "1" ]
    then
    	echo -n "Fog Service configuration in progress...";
    fi
    if [ "$STATUS_UPDATE_FOG" == "2" ]
    then
    	echo "Fog Service configuration in progress... Done";
    fi
    }
    
    #########################################################
    # Vars initialization
    #########################################################
    STATUS_UPDATE_UPDATE=0
    STATUS_UPDATE_GRUB=0
    STATUS_UPDATE_GNOME=0
    STATUS_UPDATE_FOG=0
    
    #########################################################
    # Menu
    #########################################################
    clear
    
    FUNC_TOP
    
    echo -e "\033[42m1 - Host Update (apt-get update/upgrade) 
    2 - Grub Configuration                   
    3 - Gnome Authentication                 
    4 - Enable Fog Service                   
    5 - All                                  
    6 - Exit                                 \033[0m\n"
    echo -n "Your select : " 
    read -e HOST_ACTIONS
    
    
    #########################################################
    # Host Update
    #########################################################
    if [ "$HOST_ACTIONS" == "1" ] || [ "$HOST_ACTIONS" == "5" ]
    then
    	clear
    	STATUS_UPDATE_UPDATE=1
    	FUNC_TOP
    	
    	apt-get update > /dev/null 2>&1
    	apt-get -y upgrade  > /dev/null 2>&1
    	
    	clear
    	STATUS_UPDATE_UPDATE=2
    	FUNC_TOP
    fi
    
    
    #########################################################
    # Grub Configuration (Set default boot, timeout, ...)
    #########################################################
    if [ "$HOST_ACTIONS" == "2" ] || [ "$HOST_ACTIONS" == "5" ]
    then
    	#########################################################
    	# Update Grub Configuration (Grub 2.0+)
    	#########################################################
    	
    	clear
    	STATUS_UPDATE_GRUB=1
    	FUNC_TOP
    
    	# Enabled grub-set-default
    	sed -i -e "s/\GRUB_DEFAULT=0/\GRUB_DEFAULT=saved/g" /etc/default/grub
    
    	# Set timeout at 30 seconds
    	sed -i -e "s/\GRUB_TIMEOUT=10/\GRUB_TIMEOUT=30/g" /etc/default/grub
    
    	# Removed memtest from /boot
    	rm /boot/memtest*  > /dev/null 2>&1
    
    	# Update Entry
    	update-grub2  > /dev/null 2>&1
    
    	# Set default entry from Eric Zhiqiang Ma (http://www.ericzma.com)
    	grep "^menuentry" /boot/grub/grub.cfg | cut -d "'" -f2 >/tmp/grub2-select.entries
    
    	items=`cat /tmp/grub2-select.entries`
    
    	linen=`cat /tmp/grub2-select.entries | wc -l`
    	
    	echo "\n"
    	
    	j=0
    	while [ $j -lt $linen ]
    	do
    		let "j=j+1"
    		echo -n "$j  "
    		echo "$items" | head -n $j | tail -n1
    	done
    
    	echo -n -e "\nYour select: "
    	read sel
    
    	selected=`echo "$items" | head -n $sel | tail -n1`
    
    	grub-set-default "$selected"
    	grub-mkconfig -o /boot/grub/grub.cfg  > /dev/null 2>&1
    	
    	clear
    	STATUS_UPDATE_GRUB=2
    	FUNC_TOP
    fi
    
    
    
    #########################################################
    # Gnome Authentication (Disable login history)
    #########################################################
    if [ "$HOST_ACTIONS" == "3" ] || [ "$HOST_ACTIONS" == "5" ]
    then
    	#########################################################
    	# Update Gnome Authentication
    	#########################################################
    	
    	clear
    	STATUS_UPDATE_GNOME=1
    	FUNC_TOP
    	
    	echo "greeter-hide-users=true
    greeter-show-manual-login=true" >> /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf
    
    	clear
    	STATUS_UPDATE_GNOME=2
    	FUNC_TOP
    	
    	fi
    
    #########################################################
    # Enable Fog Service
    #########################################################
    if [ "$HOST_ACTIONS" == "3" ] || [ "$HOST_ACTIONS" == "5" ]
    then
    	
    	clear
    	STATUS_UPDATE_FOG=1
    	FUNC_TOP
    	
    	#########################################################
    	# Install/update mysql-client
    	#########################################################
    	apt-get -y install mysql-client  > /dev/null 2>&1
    
    	#########################################################
    	# Copy/update rename_host_fog script in init.d
    	#########################################################
    	if [ -f /etc/init.d/rename_host_fog ]
    	then
    		rm /etc/init.d/rename_host_fog
    	fi
    	cp rename_host_fog /etc/init.d/rename_host_fog
    
    	#########################################################
    	# Update rename_host_fog rights
    	#########################################################
    	chmod 755 /etc/init.d/rename_host_fog
    
    	#########################################################
    	# Enable rename_host_fog service at startup
    	#########################################################
    	/usr/lib/insserv/insserv rename_host_fog
    	
    	clear
    	STATUS_UPDATE_FOG=2
    	FUNC_TOP
    	
    fi
    
    #########################################################
    # Restart host
    #########################################################
    if [ "$HOST_ACTIONS" == "1" ] || [ "$HOST_ACTIONS" == "2" ] || [ "$HOST_ACTIONS" == "3" ] || [ "$HOST_ACTIONS" == "4" ] || [ "$HOST_ACTIONS" == "5" ]
    then
    	
    	clear
    	FUNC_TOP
    	
    	echo -e "\n\n"
    	echo -e -n "\033[41mRestart host ? (y/N) : \033[0m"; read -e HOST_RESTART
    	
    	clear
    	FUNC_END
    	
    	if [ "$HOST_RESTART" == "y" ] || [ "$HOST_RESTART" == "Y" ]
    	then
    		sleep 3
    		grub-reboot 0
    		/sbin/init 6
    	fi
    else
    	clear
    	exit
    fi
    

    host_rename_fog

    #!/bin/bash
    #########################################################
    # Rename services with FoG, need mysql-client installed
    #########################################################
    # ch3i - 09/11/2015
    #########################################################
    
    ### BEGIN INIT INFO
    # Provides:          rename_host_fog
    # Required-Start:    $all
    # Required-Stop:     
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Rename host at boot-shutdown
    # Description:       Rename host using FoG
    ### END INIT INFO
    
    #########################################################
    # Functions
    #########################################################
    FUNC_DOTS() {
        max=65
        if [ -n "$1" ]; then
    		n=`expr $max - ${#1}`
    		echo -n " * ${1:0:max}"
    		if [ "$n" -gt 0 ]; then
    			for i in $(seq $n); do
    				printf %s .
    			done
    		fi
    	fi
    }
    
    #########################################################
    # Configuration
    #########################################################
    FOG_SERVER="server_ip"
    FOG_DATABASE_NAME="fog"
    FOG_USER="simple_select_user"
    FOG_PASSWORD="simple_select_user_password"
    
    #########################################################
    # Check Mysql Client installation
    #########################################################
    FUNC_DOTS "Check Mysql Configuration"
    if [ $(dpkg-query -W -f='${Status}' mysql-client* 2>/dev/null | grep -c "ok installed") -eq 0 ];
    then
      apt-get install -y mysql-client > /dev/null 2>&1
    fi
    echo "Done"
    
    #########################################################
    # Check Mysql Server status
    #########################################################
    FUNC_DOTS "Check FOG Mysql Database Status"
    mysqladmin -h $FOG_SERVER -u$FOG_USER -p$FOG_PASSWORD processlist > /dev/null 2>&1
    if [ $? -eq 1 ]
    then
    	echo "Error"
    	FUNC_DOTS "Failed to connect to server"
    	echo "Exiting"
    	sleep 3
    	exit
    else
    	echo "Done"
    	
    	#########################################################
    	# Get interfaces
    	#########################################################
    	NETWORK_CARDS=($(ls /sys/class/net | grep eth))
    
    	#########################################################
    	# Get host name and domain from FoG database
    	#########################################################
    	FUNC_DOTS "Checking hostname/domain configurations"
    	for ETH in ${NETWORK_CARDS[*]}
    	do
    			MAC=$(cat /sys/class/net/$ETH/address)
    			TEMP_HOST_NAME=$(mysql --host=$FOG_SERVER --user=$FOG_USER --password=$FOG_PASSWORD $FOG_DATABASE_NAME -se "SELECT hosts.hostname FROM hosts INNER JOIN hostMAC ON ( hosts.hostID = hostMAC.hmHostID ) WHERE hostMAC.hmMAC = '$MAC'");
    			if [ "$TEMP_HOST_NAME" != "" ]
    			then
    				HOST_NAME=$TEMP_HOST_NAME
    				HOST_DOMAIN_NAME=$(mysql --host=$FOG_SERVER --user=$FOG_USER --password=$FOG_PASSWORD $FOG_DATABASE_NAME -se "SELECT hosts.hostADDomain FROM hosts INNER JOIN hostMAC ON ( hosts.hostID = hostMAC.hmHostID ) WHERE hostMAC.hmMAC = '$MAC'")
    				echo "Done ($HOST_NAME.$HOST_DOMAIN_NAME)"
    				sleep 3
    				# A hostname is found - quit the loop
    				break
    			else
    				echo "Error"
    				FUNC_DOTS "Host not found in FOG database"
    				echo "exiting"
    				sleep 3
    				exit
    			fi
    	done
    
    	#########################################################
    	# Check host configuration
    	#########################################################
    	ACTUAL_FQDN=$(hostname --fqdn)
    	if [ "$HOST_NAME.$HOST_DOMAIN_NAME" != "$ACTUAL_FQDN" -a "$HOST_NAME" != "" -a "$HOST_DOMAIN_NAME" != "" ]
    	then
    	
    		#########################################################
    		# Update hostname file
    		#########################################################
    		FUNC_DOTS "Updating /etc/hostname"
    		echo $HOST_NAME >/etc/hostname
    		echo "Done"
    
    		#########################################################
    		# Update hosts file
    		#########################################################
    		FUNC_DOTS "Updating /etc/hosts"
    		echo "127.0.0.1       localhost" > /etc/hosts
    		echo "127.0.1.1       $HOST_NAME.$HOST_DOMAIN_NAME $HOST_NAME " >> /etc/hosts
    		echo "# The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts" >> /etc/hosts
    		echo "Done"
    		
    		#########################################################
    		# Update interfaces file
    		#########################################################
    		FUNC_DOTS "Updating /etc/network/interfaces"
    		echo "# This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    # The loopback network interface
    auto lo
    iface lo inet loopback" > /etc/network/interfaces
    
    		for ETH in ${NETWORK_CARDS[*]}
    		do
    			if [ "$ETH" != "lo" ]
    			then
    			echo "# $ETH Interface
    allow-hotplug $ETH
    iface $ETH inet dhcp" >> /etc/network/interfaces
    			fi
    		done
    		echo "Done"
    
    		#########################################################
    		# Clear persistent network cards
    		#########################################################
    		FUNC_DOTS "Clearing persistent networks cards"
    		if [ -f /etc/udev/rules.d/70-persistent-net.rules ]
    		then
    			rm /etc/udev/rules.d/70-persistent-net.rules
    		fi
    		echo "Done"
    
    		#########################################################
    		# Logs and Reboot machine
    		#########################################################
    		FUNC_DOTS "Building rename logs"
    		echo "$(date) ::: Update successful with Hostname : $HOST_NAME and Domain : $HOST_DOMAIN_NAME" >> /var/log/rename_host_fog.log
    		echo "Done"
    		grub-reboot 0
    		/sbin/init 6
    		exit		
    	fi
    	if [ "$HOST_NAME" == "" -a "$HOST_DOMAIN_NAME" == "" ]
    	then
    		echo "$(date) ::: Update failed : Failed to connect to Mysql Server" >> /var/log/rename_host_fog.log
    	fi
    	if [ "$HOST_NAME" != "" -a "$HOST_DOMAIN_NAME" == "" ]
    	then
    		echo "$(date) ::: Update failed : Domain name is missing" >> /var/log/rename_host_fog.log
    	fi
    fi
    exit
    

    host_rename_fog (without using of MySQL) :

    #!/bin/bash
    #########################################################
    # Rename services with FoG, need mysql-client installed
    #########################################################
    # ch3i - 09/11/2015
    #########################################################
    
    ### BEGIN INIT INFO
    # Provides:          rename_host_fog
    # Required-Start:    $all
    # Required-Stop:     
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Rename host at boot-shutdown
    # Description:       Rename host using FoG
    ### END INIT INFO
    
    #########################################################
    # Functions
    #########################################################
    FUNC_DOTS() {
        max=65
        if [ -n "$1" ]; then
    		n=`expr $max - ${#1}`
    		echo -n " * ${1:0:max}"
    		if [ "$n" -gt 0 ]; then
    			for i in $(seq $n); do
    				printf %s .
    			done
    		fi
    	fi
    }
    
    #########################################################
    # Configuration
    #########################################################
    FOG_SERVER="ip_of_server"
    FOG_WEBROOT="fog"
    
    #########################################################
    # Check FOG Server Status
    #########################################################
    FUNC_DOTS "Check FOG web service Status"
    wget "http://$FOG_SERVER/$FOG_WEBROOT/index.php" -t 1 2>/dev/null
    if [ $? -eq 0 ]
    then
    	echo "Done"
    	
    	#########################################################
    	# Get interfaces
    	#########################################################
    	NETWORK_CARDS=($(ls /sys/class/net | grep eth))
    
    	#########################################################
    	# Get host name and domain from FoG database
    	#########################################################
    	FUNC_DOTS "Checking hostname/domain configurations"
    	for ETH in ${NETWORK_CARDS[*]}
    	do
    			# read mac address
    			MAC=$(cat /sys/class/net/$ETH/address)
    			# get mac address information from fog web server
    			wget http://$FOG_SERVER/$FOG_WEBROOT/service/hostname.php?mac=$MAC -O /tmp/hostname_check 2>/dev/null
    			HOST_NAME="$(grep 'ok=' /tmp/hostname_check | cut -d "=" -f2)"
    			HOST_DOMAIN_NAME="$(grep 'ADDom' /tmp/hostname_check | cut -d "=" -f2)"
    			if [ "$HOST_NAME" != "" ]
    			then
    				echo "Done ($HOST_NAME.$HOST_DOMAIN_NAME)"
    				sleep 3
    				# A hostname is found - quit the loop
    				break
    			else
    				echo "Error"
    				FUNC_DOTS "Host not found in FOG database"
    				echo "exiting"
    				sleep 3
    				exit
    			fi
    	done
    
    	#########################################################
    	# Check host configuration
    	#########################################################
    	ACTUAL_FQDN=$(hostname --fqdn)
    	if [ "$HOST_NAME.$HOST_DOMAIN_NAME" != "$ACTUAL_FQDN" -a "$HOST_NAME" != "" -a "$HOST_DOMAIN_NAME" != "" ]
    	then
    	
    		#########################################################
    		# Update hostname file
    		#########################################################
    		FUNC_DOTS "Updating /etc/hostname"
    		echo $HOST_NAME >/etc/hostname
    		echo "Done"
    
    		#########################################################
    		# Update hosts file
    		#########################################################
    		FUNC_DOTS "Updating /etc/hosts"
    		echo "127.0.0.1       localhost" > /etc/hosts
    		echo "127.0.1.1       $HOST_NAME.$HOST_DOMAIN_NAME $HOST_NAME " >> /etc/hosts
    		echo "# The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts" >> /etc/hosts
    		echo "Done"
    		
    		#########################################################
    		# Update interfaces file
    		#########################################################
    		FUNC_DOTS "Updating /etc/network/interfaces"
    		echo "# This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    # The loopback network interface
    auto lo
    iface lo inet loopback" > /etc/network/interfaces
    
    		for ETH in ${NETWORK_CARDS[*]}
    		do
    			if [ "$ETH" != "lo" ]
    			then
    			echo "# $ETH Interface
    allow-hotplug $ETH
    iface $ETH inet dhcp" >> /etc/network/interfaces
    			fi
    		done
    		echo "Done"
    
    		#########################################################
    		# Clear persistent network cards
    		#########################################################
    		FUNC_DOTS "Clearing persistent networks cards"
    		if [ -f /etc/udev/rules.d/70-persistent-net.rules ]
    		then
    			rm /etc/udev/rules.d/70-persistent-net.rules
    		fi
    		echo "Done"
    
    		#########################################################
    		# Logs and Reboot machine
    		#########################################################
    		FUNC_DOTS "Building rename logs"
    		echo "$(date) ::: Update successful with Hostname : $HOST_NAME and Domain : $HOST_DOMAIN_NAME" >> /var/log/rename_host_fog.log
    		echo "Done"
    		grub-reboot 0
    		/sbin/init 6
    		exit		
    	fi
    	if [ "$HOST_NAME" == "" -a "$HOST_DOMAIN_NAME" == "" ]
    	then
    		echo "$(date) ::: Update failed : Failed to connect to Mysql Server" >> /var/log/rename_host_fog.log
    	fi
    	if [ "$HOST_NAME" != "" -a "$HOST_DOMAIN_NAME" == "" ]
    	then
    		echo "$(date) ::: Update failed : Domain name is missing" >> /var/log/rename_host_fog.log
    	fi
    else
    	echo "Error"
    	FUNC_DOTS "Failed to connect to server"
    	echo "Exiting"
    	sleep 3
    	exit
    fi
    exit
    

  • Moderator

    Below the version without using mysql :

    #!/bin/bash
    #########################################################
    # Rename services with FoG, need mysql-client installed
    #########################################################
    # ch3i - 09/11/2015
    #########################################################
    
    ### BEGIN INIT INFO
    # Provides:          rename_host_fog
    # Required-Start:    $all
    # Required-Stop:     
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Rename host at boot-shutdown
    # Description:       Rename host using FoG
    ### END INIT INFO
    
    #########################################################
    # Functions
    #########################################################
    FUNC_DOTS() {
        max=65
        if [ -n "$1" ]; then
    		n=`expr $max - ${#1}`
    		echo -n " * ${1:0:max}"
    		if [ "$n" -gt 0 ]; then
    			for i in $(seq $n); do
    				printf %s .
    			done
    		fi
    	fi
    }
    
    #########################################################
    # Configuration
    #########################################################
    FOG_SERVER="ip_of_server"
    FOG_WEBROOT="fog"
    
    #########################################################
    # Check FOG Server Status
    #########################################################
    FUNC_DOTS "Check FOG web service Status"
    wget "http://$FOG_SERVER/$FOG_WEBROOT/index.php" -t 1 2>/dev/null
    if [ $? -eq 0 ]
    then
    	echo "Done"
    	
    	#########################################################
    	# Get interfaces
    	#########################################################
    	NETWORK_CARDS=($(ls /sys/class/net | grep eth))
    
    	#########################################################
    	# Get host name and domain from FoG database
    	#########################################################
    	FUNC_DOTS "Checking hostname/domain configurations"
    	for ETH in ${NETWORK_CARDS[*]}
    	do
    			# read mac address
    			MAC=$(cat /sys/class/net/$ETH/address)
    			# get mac address information from fog web server
    			wget http://$FOG_SERVER/$FOG_WEBROOT/service/hostname.php?mac=$MAC -O /tmp/hostname_check 2>/dev/null
    			HOST_NAME="$(grep 'ok=' /tmp/hostname_check | cut -d "=" -f2)"
    			HOST_DOMAIN_NAME="$(grep 'ADDom' /tmp/hostname_check | cut -d "=" -f2)"
    			if [ "$HOST_NAME" != "" ]
    			then
    				echo "Done ($HOST_NAME.$HOST_DOMAIN_NAME)"
    				sleep 3
    				# A hostname is found - quit the loop
    				break
    			else
    				echo "Error"
    				FUNC_DOTS "Host not found in FOG database"
    				echo "exiting"
    				sleep 3
    				exit
    			fi
    	done
    
    	#########################################################
    	# Check host configuration
    	#########################################################
    	ACTUAL_FQDN=$(hostname --fqdn)
    	if [ "$HOST_NAME.$HOST_DOMAIN_NAME" != "$ACTUAL_FQDN" -a "$HOST_NAME" != "" -a "$HOST_DOMAIN_NAME" != "" ]
    	then
    	
    		#########################################################
    		# Update hostname file
    		#########################################################
    		FUNC_DOTS "Updating /etc/hostname"
    		echo $HOST_NAME >/etc/hostname
    		echo "Done"
    
    		#########################################################
    		# Update hosts file
    		#########################################################
    		FUNC_DOTS "Updating /etc/hosts"
    		echo "127.0.0.1       localhost" > /etc/hosts
    		echo "127.0.1.1       $HOST_NAME.$HOST_DOMAIN_NAME $HOST_NAME " >> /etc/hosts
    		echo "# The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts" >> /etc/hosts
    		echo "Done"
    		
    		#########################################################
    		# Update interfaces file
    		#########################################################
    		FUNC_DOTS "Updating /etc/network/interfaces"
    		echo "# This file describes the network interfaces available on your system
    # and how to activate them. For more information, see interfaces(5).
    
    # The loopback network interface
    auto lo
    iface lo inet loopback" > /etc/network/interfaces
    
    		for ETH in ${NETWORK_CARDS[*]}
    		do
    			if [ "$ETH" != "lo" ]
    			then
    			echo "# $ETH Interface
    allow-hotplug $ETH
    iface $ETH inet dhcp" >> /etc/network/interfaces
    			fi
    		done
    		echo "Done"
    
    		#########################################################
    		# Clear persistent network cards
    		#########################################################
    		FUNC_DOTS "Clearing persistent networks cards"
    		if [ -f /etc/udev/rules.d/70-persistent-net.rules ]
    		then
    			rm /etc/udev/rules.d/70-persistent-net.rules
    		fi
    		echo "Done"
    
    		#########################################################
    		# Logs and Reboot machine
    		#########################################################
    		FUNC_DOTS "Building rename logs"
    		echo "$(date) ::: Update successful with Hostname : $HOST_NAME and Domain : $HOST_DOMAIN_NAME" >> /var/log/rename_host_fog.log
    		echo "Done"
    		grub-reboot 0
    		/sbin/init 6
    		exit		
    	fi
    	if [ "$HOST_NAME" == "" -a "$HOST_DOMAIN_NAME" == "" ]
    	then
    		echo "$(date) ::: Update failed : Failed to connect to Mysql Server" >> /var/log/rename_host_fog.log
    	fi
    	if [ "$HOST_NAME" != "" -a "$HOST_DOMAIN_NAME" == "" ]
    	then
    		echo "$(date) ::: Update failed : Domain name is missing" >> /var/log/rename_host_fog.log
    	fi
    else
    	echo "Error"
    	FUNC_DOTS "Failed to connect to server"
    	echo "Exiting"
    	sleep 3
    	exit
    fi
    exit
    

    More slow because 10 seconds timeout in hostname.php request.


  • Moderator

    @ch3i said:

    Sleep on hostname.php is removed, I’ll try to post a version with hostname.php instead MySQL request.

    @Tom-Elliott It seem to be back


  • Moderator

    Update host_rename_fog :

    • Add the “dots” function used in FOG scripts like the installer (more sexy lol)
    • Checking Mysql status
    • Update MAC/Hostname checking (a better compatibility with multiple eth cards)

    To use that script please create a user in mysql database with select right only on tables : hosts and hostMAC !


  • Moderator

    @Jbob said:

    2.0? At the rate I’m going I’ll have Linux and Mac released for 1.3.0

    Cool, with 2.0 we have an Android/iOS client ? :p


  • Senior Developer

    2.0? At the rate I’m going I’ll have Linux and Mac released for 1.3.0


  • Moderator

    @ch3i True, true. @Jbob says that the client complies and runs fine on Linux… which is pretty amazing.


  • Moderator

    @Wayne-Workman said:

    @ch3i said:

    Sleep on hostname.php is removed, I’ll try to post a version with hostname.php instead MySQL request.

    IMHO, I’d say leave it as a request to MySQL. FOG 2.0 will not use PHP, it’ll use nodeJS and any PHP-based scripts out there will then stop working with FOG 2.0.

    I know :p But I think in FOG 2.0 we have a linux client :D :D


  • Moderator

    @ch3i said:

    Sleep on hostname.php is removed, I’ll try to post a version with hostname.php instead MySQL request.

    IMHO, I’d say leave it as a request to MySQL. FOG 2.0 will not use PHP, it’ll use nodeJS and any PHP-based scripts out there will then stop working with FOG 2.0.


  • Moderator

    Sleep on hostname.php is removed, I’ll try to post a version with hostname.php instead MySQL request.


  • Moderator

    @Tom-Elliott said:

    @ch3i the sleep is an attempt to give the client system, booted into windows, a kind of delay so winmgmt has to to start up.

    It’s possible to change sleep(10) with something like that :

    $_REQUEST['delay'] ? sleep($_REQUEST['delay']) : sleep(10);
    

    To specify delay using that :

    <fog-ip>/fog/service/hostname.php?mac=f8:bc:12:6e:67:78&newService=0&delay=3
    

  • Moderator


  • Senior Developer

    @ch3i the sleep is an attempt to give the client system, booted into windows, a kind of delay so winmgmt has to to start up.


  • Moderator

    @Junkhacker said:

    is there a reason you chose to use a mysql query instead of using the built in mechanism of an http request to

    <fog-ip>/fog/service/hostname.php?mac=f8:bc:12:6e:67:78&newService=0 
    

    using the http request, you wouldn’t need to utilize database credentials

    Hi,

    I’ve tested with hostname.php it’s functional but very long…

    Why there is a sleep during 10 seconds ?

    sleep(10);
        // Make system wait ten seconds before sending data
    

  • Moderator

    @ch3i Wow you have been busy, very nice work!


  • Moderator

    @cspence When I’ve wrote that script I only used FOG Database… But to minimize security problem I’ve created a read only mysql user.
    If you can help me than @Junkhacker to optimize my script or create a FOG client for Linux, you are welcome lol.

    @Junkhacker I’m trying to optimize security (I’ve discover service/class files on fog since 2/3 months), I’ll post a “FOG client” for linux (Redhat/Debian based during this month)


  • Developer

    @Junkhacker said:

    is there a reason you chose to use a mysql query instead of using the built in mechanism of an http request to

    <fog-ip>/fog/service/hostname.php?mac=f8:bc:12:6e:67:78&newService=0 
    

    using the http request, you wouldn’t need to utilize database credentials

    Maybe he likes to live dangerously. :sunglasses:


  • Developer

    is there a reason you chose to use a mysql query instead of using the built in mechanism of an http request to

    <fog-ip>/fog/service/hostname.php?mac=f8:bc:12:6e:67:78&newService=0 
    

    using the http request, you wouldn’t need to utilize database credentials


  • Moderator

    I forgot to tell you that you could choose to only install the “fog service” :

    cmm.PNG


  • Moderator

    Post updated due to new Forum.
    Enjoy ;)


Log in to reply
 

353
Online

39.3k
Users

11.0k
Topics

104.4k
Posts

Looks like your connection to FOG Project was lost, please wait while we try to reconnect.