• Recent
    • Unsolved
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    [SCRIPT] Auto rename debian machine

    Scheduled Pinned Locked Moved
    General
    1
    4
    2.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ch3iC
      ch3i Moderator
      last edited by

      Hi,

      I made a script launch as a service to update debian machine informations :

      • /etc/hostname
      • /etc/hosts
      • /etc/dhcp/dhclient.conf

      My rename_host_fog file

      [CODE]#!/bin/bash
      #########################################################

      Rename services with FoG, need mysql-client installed

      #########################################################

      ch3i - 02/03/2015

      #########################################################

      BEGIN INIT INFO

      Provides: rename_host

      Required-Start: $remote_fs $syslog $network

      Required-Stop: $remote_fs $syslog $network

      Default-Start: 2 3 4 5

      Default-Stop: 0 1 6

      Short-Description: Rename host at boot

      Description: Rename host using FoG

      END INIT INFO

      #########################################################

      Configuration

      #########################################################
      FOG_SERVER=“xxx.xxx.xxx.xxx”
      FOG_DATABASE_NAME=“fog”
      FOG_USER=“user_with_select_right”
      FOG_PASSWORD=“password”

      #########################################################

      Get MAC address (3 cards max)

      #########################################################
      if [ -f /sys/class/net/eth0/address ]
      then
      MAC0=$(cat /sys/class/net/eth0/address)
      fi
      if [ -f /sys/class/net/eth1/address ]
      then
      MAC1=$(cat /sys/class/net/eth1/address)
      fi
      if [ -f /sys/class/net/eth2/address ]
      then
      MAC2=$(cat /sys/class/net/eth2/address)
      fi

      #########################################################

      Get machine name and domain from FoG database

      #########################################################
      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 = ‘$MAC0’ OR hostMAC.hmMAC = ‘$MAC1’ OR hostMAC.hmMAC = ‘$MAC2’”);
      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 = ‘$MAC0’ OR hostMAC.hmMAC = ‘$MAC1’ OR hostMAC.hmMAC = ‘$MAC2’”);

      #########################################################

      Check machine configuration

      #########################################################
      ACTUAL_FQDN=$(hostname --fqdn)
      if [ “$HOST_NAME.$HOST_DOMAIN_NAME” != “$ACTUAL_FQDN” ]
      then
      #########################################################

      Update hostname file

      #########################################################
      echo $HOST_NAME >/etc/hostname

      #########################################################

      Update hosts file

      #########################################################
      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

      #########################################################

      Update dhclient file

      #########################################################
      cp /etc/dhcp/dhclient.conf /tmp
      sed s/<hostname>/$HOST_NAME/ /tmp/dhclient.conf > /etc/dhcp/dhclient.conf

      #########################################################

      Clear persistent network cards

      #########################################################
      if [ -f /etc/udev/rules.d/70-persistent-net.rules ]
      then
      rm /etc/udev/rules.d/70-persistent-net.rules
      fi

      #########################################################

      Reboot machine

      #########################################################
      init 6
      fi
      [/CODE]

      My sysprep_fog file used to enable rename_host_fog services

      [CODE]#!/bin/bash
      #########################################################

      Sysprep to enable rename_host_fog service

      #########################################################

      ch3i - 02/03/2015

      #########################################################

      #########################################################

      Install/update mysql-client

      #########################################################
      apt-get -y install mysql-client

      #########################################################

      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

      #########################################################
      insserv rename_host_fog

      #########################################################

      Reboot machine

      #########################################################
      init 6
      [/CODE]

      The service is OK and update automatically my machine at boot if I modified information into FoG server, but won’t reboot 😕

      Does anyone can help me ?

      Regards,
      Ch3i.

      1 Reply Last reply Reply Quote 0
      • ch3iC
        ch3i Moderator
        last edited by

        I solve my problem changing the required start.

        [CODE]### 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

        Description: Rename host using FoG

        END INIT INFO[/CODE]

        I improve the script by check if the FoG server respond.

        [CODE]if [ “$HOST_NAME.$HOST_DOMAIN_NAME” != “$ACTUAL_FQDN” ] && [ “$HOST_NAME” != “” ] && [ “$HOST_DOMAIN_NAME” != “” ][/CODE]

        Now the service check the name at boot and at shutdown.

        Below the new version :

        rename_host_fog

        [CODE]#!/bin/bash
        #########################################################

        Rename services with FoG, need mysql-client installed

        #########################################################

        ch3i - 02/03/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

        Description: Rename host using FoG

        END INIT INFO

        #########################################################

        Configuration

        #########################################################
        FOG_SERVER=“xxx.xxx.xxx.xxx”
        FOG_DATABASE_NAME=“fog”
        FOG_USER=“user_with_select_right”
        FOG_PASSWORD=“password”

        #########################################################

        Get MAC address (3 cards max)

        #########################################################
        if [ -f /sys/class/net/eth0/address ]
        then
        MAC0=$(cat /sys/class/net/eth0/address)
        fi
        if [ -f /sys/class/net/eth1/address ]
        then
        MAC1=$(cat /sys/class/net/eth1/address)
        fi
        if [ -f /sys/class/net/eth2/address ]
        then
        MAC2=$(cat /sys/class/net/eth2/address)
        fi

        #########################################################

        Get machine name from FoG database

        #########################################################
        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 = ‘$MAC0’ OR hostMAC.hmMAC = ‘$MAC1’ OR hostMAC.hmMAC = ‘$MAC2’”);
        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 = ‘$MAC0’ OR hostMAC.hmMAC = ‘$MAC1’ OR hostMAC.hmMAC = ‘$MAC2’”);

        #########################################################

        Check machine configuration

        #########################################################
        ACTUAL_FQDN=$(hostname --fqdn)
        if [ “$HOST_NAME.$HOST_DOMAIN_NAME” != “$ACTUAL_FQDN” ] && [ “$HOST_NAME” != “” ] && [ “$HOST_DOMAIN_NAME” != “” ]
        then
        echo “update”
        #########################################################

        Update hostname file

        #########################################################
        echo $HOST_NAME >/etc/hostname

        #########################################################

        Update hosts file

        #########################################################
        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

        #########################################################

        Update dhclient file

        #########################################################
        cp /etc/dhcp/dhclient.conf /tmp
        sed s/<hostname>/$HOST_NAME/ /tmp/dhclient.conf > /etc/dhcp/dhclient.conf

        #########################################################

        Clear persistent network cards

        #########################################################
        if [ -f /etc/udev/rules.d/70-persistent-net.rules ]
        then
        rm /etc/udev/rules.d/70-persistent-net.rules
        fi

        #########################################################

        Reboot machine

        #########################################################
        echo “$(date) ::: Update successful with Hostname : $HOST_NAME and Domain : $HOST_DOMAIN_NAME” >> /var/log/rename_host_fog.log
        /sbin/init 6
        fi
        if [ “$HOST_NAME” == “” ] && [ “$HOST_DOMAIN_NAME” == “” ]
        then
        echo “$(date) ::: Update failed : Failed to connect to Mysql Server” >> /var/log/rename_host_fog.log
        fi
        if [ “$HOST_NAME” != “” ] && [ “$HOST_DOMAIN_NAME” == “” ]
        then
        echo “$(date) ::: Update failed : Domain name is missing” >> /var/log/rename_host_fog.log
        fi
        [/CODE]

        sysprep_fog

        [CODE]#!/bin/bash
        #########################################################

        Sysprep to enable rename_host_fog service

        #########################################################

        ch3i - 02/03/2015

        #########################################################

        #########################################################

        Install/update mysql-client

        #########################################################
        apt-get -y install mysql-client

        #########################################################

        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

        #########################################################
        insserv rename_host_fog

        #########################################################

        Reboot machine

        #########################################################
        /sbin/init 6[/CODE]

        To use it, you have to :

        create two files in the same folder :

        • rename_host_fog
        • sysprep_fog

        change sysprep_fog rights : chmod 755 sysprep_fog.
        enable service with : ./sysprep.fog

        You can check log in : /var/log/rename_host_fog.log

        1 Reply Last reply Reply Quote 0
        • ch3iC
          ch3i Moderator
          last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • ch3iC
            ch3i Moderator
            last edited by

            Hi,

            I updated the script :

            • no limitation of number of ethernet card
            • update /etc/network/interfaces

            [CODE]#!/bin/bash
            #########################################################

            Rename services with FoG, need mysql-client installed

            #########################################################

            ch3i - 02/05/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

            Description: Rename host using FoG

            END INIT INFO

            #########################################################

            Configuration

            #########################################################
            FOG_SERVER=“xxx.xxx.xxx.xxx”
            FOG_DATABASE_NAME=“fog”
            FOG_USER=“user_with_select_right”
            FOG_PASSWORD=“password”

            #########################################################

            Get interfaces

            #########################################################
            NETWORK_CARDS=($(ls /sys/class/net/))

            #########################################################

            Get host name and domain from FoG database

            #########################################################
            for ETH in ${NETWORK_CARDS[*]}
            do
            if [ “$ETH” != “lo” ]
            then
            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’”);
            fi
            fi
            done

            #########################################################

            Check host configuration

            #########################################################
            ACTUAL_FQDN=$(hostname --fqdn)
            if [ “$HOST_NAME.$HOST_DOMAIN_NAME” != “$ACTUAL_FQDN” ] && [ “$HOST_NAME” != “” ] && [ “$HOST_DOMAIN_NAME” != “” ]
            then
            #########################################################
            # Update hostname file
            #########################################################
            echo $HOST_NAME >/etc/hostname

            #########################################################
            # Update hosts file
            #########################################################
            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

            #########################################################
            # Update interfaces file
            #########################################################
            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

            #########################################################
            # Clear persistent network cards
            #########################################################
            if [ -f /etc/udev/rules.d/70-persistent-net.rules ]
            then
                rm /etc/udev/rules.d/70-persistent-net.rules
            fi
            
            #########################################################
            # Logs and Reboot machine
            #########################################################
            echo "$(date) ::: Update successful with Hostname : $HOST_NAME and Domain : $HOST_DOMAIN_NAME" >> /var/log/rename_host_fog.log
            /sbin/init 6
            

            fi
            if [ “$HOST_NAME” == “” ] && [ “$HOST_DOMAIN_NAME” == “” ]
            then
            echo “$(date) ::: Update failed : Failed to connect to Mysql Server” >> /var/log/rename_host_fog.log
            fi
            if [ “$HOST_NAME” != “” ] && [ “$HOST_DOMAIN_NAME” == “” ]
            then
            echo “$(date) ::: Update failed : Domain name is missing” >> /var/log/rename_host_fog.log
            fi
            [/CODE]

            Regards,
            Ch3i.

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post

            184

            Online

            12.0k

            Users

            17.3k

            Topics

            155.2k

            Posts
            Copyright © 2012-2024 FOG Project