rhel5+dhcp+vsftp+pxe+tftp+kickstart環境配置腳本

#!/bin/bash
# -*- coding: utf-8 -*-
#
#  dfkp5.sh
#
#  Copyright 2012 wanghui <hwang@runner>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#
#聲明全局變量
#網絡網段
NET=`ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | awk -F: '{print$2}' |cut -d. -f1,2,3`
#pxe服務器主機ip
SER_IP=`ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | awk -F: '{print$2}'`
#dhcp服務分配的起始ip
STA_IP="$NET.10"
#dhcp服務分配的結束ip
END_IP="$NET.100"
#子網掩碼
NETMASK=`ifconfig eth0 | grep Mask |awk -F: '{print $4}'`
ROUTE=`route | grep default | awk '{print $2}'`
#
#配置本地yum服務
#
#判斷yum服務是否安裝
rpm -qa | grep yum >& /dev/null
if [ $? -eq 1 ]
    then
        echo "yum is not installed! please install yum."
    else
        echo "yum is installed!"
fi
#判斷使用光盤還是iso文件做安裝鏡像
echo "-----------------------------------"
echo "1) use cdrom "
echo "2) use iso file"
echo "q) exit"
echo
echo -n "Please Enter a Installation method:"
read MET
    case $MET in
        1)echo "you slect cdrom install operationg system!"
          echo -e "\033[31m Please insert your cdrom.... \033[0m"
          sleep 20
          mount -r /dev/cdrom /mnt >& /dev/null
          mount -r /dev/cdrom /var/ftp/pub >& /dev/null
        ;;
        2)echo "you slect iso file install operationg system!"
          echo -n "Please input you file path:"
          read ISOF
          mount -o loop $ISOF /mnt >& /dev/null
          mount -o loop $ISOF /var/ftp/pub >& /dev/null 
        ;;
        q)echo "quit!"
              exit 0
        ;;
        *)echo "Please slect 1|2|q"
              exit 1
        ;;
    esac
#配置yum服務的本地yum源
cat <<EOF >/etc/yum.repos.d/server.repo
[base]
name=base
baseurl=file:///mnt/Server/
enabled=1
gpgcheck=0
EOF
#清除yum源的緩存
yum clean all
yum list >& /dev/null
if [ $? -eq 0 ]
    then
        echo "YUM 服務配置成功!"
fi
#
#判斷是否安裝了相關軟件
#
for RPM in vsftpd tftp tftp-server  system-config-kickstart
    do
        rpm -qa | grep  $RPM  >& /dev/null
        if [ $? -eq 1 ]
            then
                echo "$RPM is not installed!"
                yum install -y $RPM #>& /dev/null
                if [ $? -eq 0 ]
                    then
                        echo -e "\033[32m$RPM 已安裝成功!\033[0m"
                    else
                        echo -e "\033[31m$RPM 安裝失敗!\033[0m"
                fi
            else
                echo -e "\033[34m$RPM is installed!\033[0m"
        fi
done
#
#配置vsftp服務
#
#配置ks.cfg文件
system-config-kickstart
KSCFG=/var/ftp/ks.cfg
cp -rf ks.cfg $KSCFG
cat postshell >>$KSCFG
sed -i '/^# System language/i key  --skip' $KSCFG
sed -i "s/serverip/${SER_IP}/g" $KSCFG
/etc/init.d/vsftpd restart
#
#配置tftp服務
#
TFTP='/etc/xinetd.d/tftp'
TMP_TFTP="/tmp/tmp.$$"
sed -i '/disable/s/yes/no/' $TFTP
#mv -f $TMP_TFTP $TFTP
PXE='/usr/lib/syslinux/pxelinux.0'
ISO='/mnt/isolinux/isolinux.cfg'
IMG='/mnt/images/pxeboot/initrd.img'
VML='/mnt/images/pxeboot/vmlinuz'
DEFT='/tftpboot/pxelinux.cfg/default'
mkdir /tftpboot/pxelinux.cfg
cp $PXE  /tftpboot/
cp $ISO $DEFT
cp $IMG /tftpboot/
cp $VML /tftpboot/
chmod 644 $DEFT
sed -i "3s/600/1/;12d;11 a\   append initrd=initrd.img ks=ftp://$SER_IP/ks.cfg" $DEFT
/etc/init.d/xinetd restart
#
#配置dhcp服務
#
yum install -y dhcp
DHCP='/etc/dhcpd.conf'
TMP_DHCP="/tmp/tmp.$$"
cat <<EOF >$DHCP
ddns-update-style interim;
ignore client-updates;
subnet $NET.0 netmask $NETMASK {
    option routers          $ROUTE;
    option subnet-mask      $NETMASK;
        filename="pxelinux.0";
    next-server $SER_IP;
    range dynamic-bootp $STA_IP $END_IP;
    default-lease-time 21600;
    max-lease-time 43200;
}
EOF
#mv -f $TMP_DHCP $DHCP
/etc/init.d/dhcpd restart
#
#系統安全設置
#
iptables -F
service iptables save
service iptables stop
setenforce 0
#
#配置NFS
#
mkdir /sharedata
echo "/sharedata         *(sync,rw)">/etc/exports
service nfs restart


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章