LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash Script to change hostname for RHEL (http://www.linuxquestions.org/questions/linux-general-1/bash-script-to-change-hostname-for-rhel-856475/)

wegadnie 01-15-2011 08:24 AM

Bash Script to change hostname for RHEL
 
Hi All,

I'm trying to write a script that can help me to change the hostname of the computer at the first boot. The reason why I need this script is because I manage a number of training computer and I use Ghost to re-image it every 2 months.

The script will read the mac address and the hostname from a csv file and compare it with the mac address on the current machine. I have stuck and don't know what is wrong as I'm new with script. Any help is highly appreciated.

csv format:
Code:

00:11:22:33:44:55 host01
00:11:22:33:44:56 host02

script:
Code:

#!/bin/bash
# Description
# Change HOSTNAME for each machine from  CSV file
#
# CSV File Format
# Each line contains two columns - mac address & hostname
# Description End

export PATH=$PATH:/usr/sbin

#Set variable for script
mac_addr=`ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11`
network="/etc/sysconfig/network"
network_org="/etc/sysconfig/network.org"

#Check argument
if [ -z $1 ]; then
  echo "Usage: `basename $0` <csv file>"
  exit 1
fi

if [ -r $1 ]; then
  cat $1 |
  while read line; do
    mac_org=$(echo $line | cut -d ' ' -f2)
    hostname=$(echo $line | cut -d ' ' -f3\n)
        if [ $mac_addr = $mac_org ]; then
          sed "s/old_host/$hostname/g" $network_org > $network
        fi
  done
fi


crts 01-15-2011 11:12 AM

Hi,

can you be a bit more specific? Is the script giving an error or does it simply not change the address? How does your csv-file look like?
Code:

hostname=$(echo $line | cut -d ' ' -f3\n)
Is the '\n' at the end of the line a typo when you posted or is it actually in your script?

wegadnie 01-15-2011 08:47 PM

the "\n" is actually in my script as I want it to go to the next line at the next loop. When I run the script, it generate this error:
Code:

# ./ch_hostname.sh list_machine.csv
cut: invalid byte, character or field list
Try `cut --help' for more information.

I know that there's something wrong with the cut command at the first and second line and the cut command at second line can not get the right specific variable from the csv file.

crts 01-15-2011 10:10 PM

Quote:

Originally Posted by wegadnie (Post 4225664)
the "\n" is actually in my script as I want it to go to the next line at the next loop.

Where did you get that 'trick' from? Anyway, it doesn't work this way, as your error also tells you. You don't need the extra '\n' to process the next line. Just remove it and the error should go away.

trickykid 01-26-2011 01:21 PM

Why go through this painstaking process of reading from a csv file and maintaining such a file? Set yourself up a DNS server with DHCP that issues IP based on MAC (so each machine gets same IP or just use Static IP's). When you reimage the machine, RHEL actually has it builtin where it will do a hostname or reverse lookup on the IP, check DNS and set the hostname accordingly. If it doesn't, just write a quick script that does it for you.

A proper DNS/DHCP or IP Assigning, RHEL will do the hostname after a reimage, kickstart or reinstall for you.

wegadnie 06-19-2011 05:59 AM

Hi trickykid,

Thanks for your advise. I don't manage to the DNS server and also could not set up a new DNS server within our office. When you mention IP Assigning, do you refer to DHCP Reservation? Is it help to get the correct the hostname every single boot? Thanks.

chrism01 06-20-2011 08:42 PM

Yep, DHCP http://www.linuxtopia.org/online_boo...ng-server.html. Specifically, look at Example 20.2. Shared-network Declaration
Quote:

host apex {
option host-name "apex.example.com";
hardware ethernet 00:A0:78:8E:9E:AA;
fixed-address 192.168.1.4;
}

wegadnie 06-22-2011 05:37 AM

Thanks to all who have helped me on this. I have successfully to manage the hostname via DHCP/DNS.


All times are GMT -5. The time now is 01:11 PM.