Simple script to modify kickstart file

Hi,

I would like to create a script so that it will ask me the following:

1) What is the ip address?
2) What is the gateway address?
3) What is the hostname?

and then put the answer to the below kickstart file (kickstart.cfg)

Here I included the kickstart.cfg:

# Kickstart file automatically generated by anaconda.

install
nfs --server=10.10.10.200 --dir=/vault/redhat/rhel5/5.1/x86_64
key --skip
lang en_US.UTF-8
keyboard us
xconfig --startxonboot
network --device eth0 --bootproto static --ip 10.10.10.20 --netmask 255.255.255.0 --gateway 10.10.10.1 --nameserver
10.10.10.100--hostname abc
network --device eth1 --onboot no --bootproto dhcp --hostname abc
rootpw --iscrypted $1$r3I9vk75$JWCGlXqgYd0/55lfg09NJ.
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc America/New_York
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all --drives=sda
part /boot --fstype ext3 --size=100 --ondisk=sda
part pv.6 --size=0 --grow --ondisk=sda
volgroup VolGroup00 --pesize=32768 pv.6
logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=25600
logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=8192
logvol /abc --fstype ext2 --name=LogVol03 --vgname=VolGroup00 --size=25216
logvol /var --fstype ext3 --name=LogVol02 --vgname=VolGroup00 --size=10240

So star out and create the script.

Inside the script echo your questions and then read the answer to assign it to a variable. Ex:

echo "What is the ip address?"
read ipaddress

What is the gateway address?
read gatewayaddress

What is the hostname?
read hostname

So now you have your answers assigned to variables. Now echo each line of your kickstart file, but instead of the IP address you listed below, now replace it with $ipaddress.

Sorry. I still don't know how. Could you please send me a sample script?

Thanks alot.

[root@atlantis ~]# cat generateKickstartConfig.sh

#!/bin/sh

#define outputfile
KS_CONFIG=ks.cfg

echo -e "Starting Kickstart Configuration Generator Script ...\n"

echo -en "\tWhat is the IP Address:"
read IP_ADDRESS

echo -en "\tWhat is the gateway Address:"
read GATEWAY_ADDRESS

echo -en "\tWhat is the DNS Server:" 
read DNS_SERVER

echo -en "\tWhat is the Hostname:"
read HOSTNAME

echo "network --device eth0 --bootproto static --ip ${IP_ADDRESS} --netmask 255.255.255.0 --gateway ${GATEWAY_ADDRESS} --nameserver ${DNS_SERVER} --hostname ${HOSTNAME}" >> ${KS_CONFIG}

Remember to make the script executable
Execute with ./generateKickstartConfig.sh
Answer the 4 questions and "ls" your directory, you'll see a ks.cfg file

This is a very basic solution, there's so many possiblities, you can verify the user input to check to see if the values are valid, like if they enter an IP for hostname or vice versa, check DNS to see if these IPs resolve before using them in the kickstart, use regular expression to verify the user input, if its invalid you can loop to verify they enter something useful, I mean you can write a pretty complicated script to generate your ks.cfg file.

Thanks. I have one more question. The script above will insert another new line for the "network....". Can I make it just only add the answer to those variable in the kickstart file? Please advise. Thanks alot

# Kickstart file automatically generated by anaconda.

install
nfs --server=10.10.10.200 --dir=/redhat/install
key --skip
lang en_US.UTF-8
keyboard us
xconfig --startxonboot
network --device eth0 --bootproto static --ip $IP --netmask 255.255.255.0 --gateway $GATEWAY --nameserver 172.27.200.215,172.27.201.215 --hostname $HOSTNAME
network --device eth1 --onboot no --bootproto dhcp --hostname $HOSTNAME
rootpw --iscrypted $1$r3I9vk75$JWCGlXqgYd0/55lfg09NJ.
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled

Sure, you can make it do anything. If you're looking at building a full ks.cfg from the ground up, you can add more questions and output to a new ks.cfg. If you have an existing template you want to use and only want to chug in a few values look at using "sed" If you only want a response for this section, you can just use a "here document" and just echo the entire ks.cfg out including the user input information for network section. Like I said, you can do pretty much anything, you probably want to look at simple bash examples while you create this script.

Start to create something and post what you have and will continue to help you out.

Basically I want to copy the original kickstart file to a new file call newks.cfg and insert the "network" statement to the 9th line of the newks.cfg

Here is my script, but it doesn't work...it only add to the bottom of the line to newks.cfg. Please help.

#!/bin/sh

NEW_FILE=newks.cfg
OLD_FILE=oldks.cfg

echo -en "What is the IP Address?"
read IP

echo -en "What is the gateway Address?"
read GATEWAY

echo -en "What is the Hostname?"
read HOSTNAME

/bin/cp ${OLD_FILE} ${NEW_FILE}

echo "network --device eth0 --bootproto static --ip ${IP} --netmask 255.255.255.0 --gateway ${GATEWAY} --nameserver 172.27.200.215,10.72.201.215 --hostname ${HOSTNAME}" >> ${NEW_FILE}

You're just appending by using the redirect output (>>), you need to look at sed, and search for the area where you want the chunk of text. Man sed or look at examples online to do so.

I've taken what you guys supplied and another post I found elsewhere and combined it with some sed to make it work the way i want it to. Put this at the end of your ks.cfg file in a %pre section and it will ask you for the network information:

%pre
#!/bin/sh

chvt 3
echo "--Static Network Configuration--"
echo -en "What is the IP Address? : "
read ip

echo -en "What is the Netmask? : "
read netmask

echo -en "What is the Hostname? : "
read host

echo -en "What is the Gateway? : "
read gateway

echo -en "What is the Name Server?"
read nameserver

line="network --bootproto static --ip $ip --netmask $netmask -hostname -host --gateway $ga teway --nameserver $nameserver"
751 ks="/tmp/ks.cfg"
752 sed -e "/^network/s/^network.*/$line/" $ks > ${ks}.tmp && mv ${ks}.tmp $ks

note: line, ks, and sed are all their own line
should read
line=
ks=
sed

I modified my script even further to ask user to verify input and if correct break out and if not then ask for the information again.

--------------------------------------------
#!/bin/bash
clear
while true; do
echo "Enter Network Configuration"
echo "---------------------------"
echo " "
echo -en "Enter Network Device (eth0,eth1) : "
read device

echo -en "Enter the Hostname? : "
read host

echo -en "Enter the IP Address? : "
read ip

echo -en "Enter the Netmask? : "
read netmask

echo -en "Enter the Gateway? : "
read gateway

echo -e ""
echo -e "Please Review Your Entries "
echo -e "--------------------------"
echo -e "Network Device : $device"
echo -e "Hostname : $host"
echo -e "IP Address : $ip"
echo -e "Netmask : $netmask"
echo -e "Gateway : $gateway"
echo -e " "
echo -en "Does Everything Look Correct? (y/n) "
read yn
case $yn in
y* | Y* ) line="network --bootproto static --device $device --ip $ip --netmask $netmask --hostname $host --gateway $gateway"
ks="/tmp/ks.cfg"
sed -e "/^network/s/^network./$line/" $ks > ${ks}.tmp && mv ${ks}.tmp $ks; break;;
[nN]
) echo -e "Renter Your Information" ; continue;;
q* ) exit ;;
* ) echo "Enter yes or no" ;;
esac
done