LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-15-2011, 08:24 AM   #1
wegadnie
Member
 
Registered: Oct 2007
Posts: 48

Rep: Reputation: 15
Question 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
 
Old 01-15-2011, 11:12 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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?

Last edited by crts; 01-15-2011 at 11:41 PM.
 
Old 01-15-2011, 08:47 PM   #3
wegadnie
Member
 
Registered: Oct 2007
Posts: 48

Original Poster
Rep: Reputation: 15
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.

Last edited by wegadnie; 01-15-2011 at 08:51 PM.
 
Old 01-15-2011, 10:10 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by wegadnie View Post
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.
 
Old 01-26-2011, 01:21 PM   #5
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 194Reputation: 194
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.
 
Old 06-19-2011, 05:59 AM   #6
wegadnie
Member
 
Registered: Oct 2007
Posts: 48

Original Poster
Rep: Reputation: 15
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.

Last edited by wegadnie; 06-19-2011 at 06:00 AM.
 
Old 06-20-2011, 08:42 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
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;
}
 
Old 06-22-2011, 05:37 AM   #8
wegadnie
Member
 
Registered: Oct 2007
Posts: 48

Original Poster
Rep: Reputation: 15
Thanks to all who have helped me on this. I have successfully to manage the hostname via DHCP/DNS.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
after hostname change, syslogd still output previous hostname to messages file tiger2000 Linux - General 3 07-22-2009 09:36 PM
how to load an array from a file in RHEL bash script steven.c.banks Linux - General 3 10-03-2008 03:24 PM
Using shell script to change hostname(Fedora Core6) lion1978 Linux - Software 10 03-05-2007 02:14 AM
script to change IP and hostname under Solaris 10 xpucto Solaris / OpenSolaris 9 06-08-2006 05:37 AM
how to change user in bash script c0d3 Programming 6 07-27-2005 11:34 AM


All times are GMT -5. The time now is 02:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration