Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.