快速搭建部署网络PXE shell 脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速搭建部署网络PXE shell 脚本相关的知识,希望对你有一定的参考价值。


#!/bin/bash

#获取当前机器IP地址

IP=`ifconfig | head -2 | tail -1 | awk '{print $2}'`

#获取当前机器MASK地址

MASK=`ifconfig | head -2 | tail -1 | awk '{print $4}'`

MASK1=`echo $MASK | awk -F. '{print $1}'`

if [ $MASK1 -ne 0 ];then

IP1=`echo $IP | awk -F. '{print $1}'`

MIN1=`echo $IP | awk -F. '{print $1}'`

MAX1=`echo $IP | awk -F. '{print $1}'`

else

IP1=0

MIN1=1

MAX1=254

fi

MASK2=`echo $MASK | awk -F. '{print $2}'`

if [ $MASK2 -ne 0 ];then

        IP2=`echo $IP | awk -F. '{print $2}'`

MIN2=`echo $IP | awk -F. '{print $2}'`

MAX2=`echo $IP | awk -F. '{print $2}'`

else

        IP2=0

MIN2=1

MAX2=254

fi

MASK3=`echo $MASK | awk -F. '{print $3}'`

if [ $MASK3 -ne 0 ];then

        IP3=`echo $IP | awk -F. '{print $3}'`

MIN3=`echo $IP | awk -F. '{print $3}'`

MAX3=`echo $IP | awk -F. '{print $3}'`

else

        IP3=0

MIN3=1

MAX3=254

fi

MASK4=`echo $MASK | awk -F. '{print $4}'`

if [ $MASK4 -ne 0 ];then

        IP4=`echo $IP | awk -F. '{print $4}'`

MIN4=`echo $IP | awk -F. '{print $4}'`

MAX4=`echo $IP | awk -F. '{print $4}'`

else

        IP4=0

MIN4=1

MAX4=254

fi

#NETIP为要配置DHCP的网络号

NETIP="$IP1.$IP2.$IP3.$IP4"

#MINIP 为要配置DCHP范围最小IP

MINIP="$MIN1.$MIN2.$MIN3.$MIN4"

#MAXIP 为要配置DCHP范围最大IP

MAXIP="$MAX1.$MAX2.$MAX3.$MAX4"



systemctl stop firewalld.service 

setenforce 0

#配置本地YUM源

echo -en "正在配置本地YUM源......\t\t" 

creatyum(){

echo "/dev/cdrom  /mnt/  iso9660  defaults 0 0" >> /etc/fstab 

mount -a  &> /dev/null

n=`ls /mnt/$Date | wc -l`

if [ $n -eq 0 ];then

echo '请挂载光盘文件!!!'

exit

fi

rm -rf /etc/yum.repos.d/*

echo "[dvd]

name=dvd

baseurl=file:///mnt/

gpgcheck=0

" > /etc/yum.repos.d/dvd.repo

yum clean all &> /dev/null

}

yum repolist &> ${Date}.txt

a=`cat ${Date}.txt | tail -1 | sed 's/[,]//' | awk '{print $2}'` 

rm -rf ${Date}.txt

if [ -z `echo "$a" | grep -e 's/[^0-9]//'` ];then

creatyum

elif [ $a -eq 0 ];then

creatyum 

fi

echo -e "\e[32;1m[OK]\e[0m"




#部署DHCP服务器

echo -en "正在部署DHCP服务器......\t\t" 

yum -y install dhcp  >& /dev/null

echo '

subnet $NETIP netmask $MASK {

    range $MINIP $MAXIP; 

    option domain-name-servers $IP;

    option routers $IP;                       

    default-lease-time 600;       

    max-lease-time 7200;

    next-server $IP;

    filename "pxelinux.0";


}' > /etc/dhcp/dhcpd.conf

sed -i "s/\$IP/$IP/" /etc/dhcp/dhcpd.conf

sed -i "s/\$MASK/$MASK/" /etc/dhcp/dhcpd.conf

sed -i "s/\$NETIP/$NETIP/" /etc/dhcp/dhcpd.conf

sed -i "s/\$MINIP/$MINIP/" /etc/dhcp/dhcpd.conf

sed -i "s/\$MAXIP/$MAXIP/" /etc/dhcp/dhcpd.conf

systemctl restart dhcpd>& /dev/null

systemctl enable dhcpd>& /dev/null

echo -e "\e[32;1m[OK]\e[0m"


#搭建TFTP服务

echo -en "正在搭建TFTP服务......\t\t" 

yum -y install tftp-server.x86_64>& /dev/null

systemctl restart tftp>& /dev/null

systemctl enable tftp>& /dev/null

echo -e "\e[32;1m[OK]\e[0m"


#部署pxelinux.0文件

echo -en "正在部署pxelinux.0文件......\t\t"

yum -y install syslinux-4.05-12.el7.x86_64 >& /dev/null

cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

mkdir /var/lib/tftpboot/pxelinux.cfg/  >& /dev/null

cp /mnt/isolinux/initrd.img /mnt/isolinux/vmlinuz /mnt/isolinux/vesamenu.c32  /var/lib/tftpboot/

echo -e "\e[32;1m[OK]\e[0m"



#部署default默认的菜单文件

echo -en "正在部署default默认的菜单文件......\t\t"

yum -y install vsftpd >& /dev/null

systemctl restart vsftpd >& /dev/null

systemctl enable vsftpd >& /dev/null

mkdir /var/ftp/rhel7  >& /dev/null

mount /dev/cdrom /var/ftp/rhel7 >& /dev/null

echo -e "\e[32;1m[OK]\e[0m"


#搭建vsftpd服务,共享应答文件

echo -en "正在搭建vsftpd服务,共享应答文件......\t\t"

echo '

#platform=x86, AMD64, 或 Intel EM64T

#version=DEVEL

# Install OS instead of upgrade

install

# Keyboard layouts

keyboard 'us'

# Root password

rootpw --iscrypted $1$Vl9/l0oR$ZM8s/NJIjlPSGy6P0cMMb.

# System timezone

timezone Asia/Shanghai

# Use network installation

url --url="ftp://$IP/rhel7"

# System language

lang zh_CN

# Firewall configuration

firewall --disabled

# System authorization information

auth  --useshadow  --passalgo=sha512

# Use graphical install

graphical

# SELinux configuration

selinux --disabled

# Do not configure the X Window System

skipx


# Network information

network  --bootproto=dhcp --device=eth0

# Reboot after installation

reboot

# System bootloader configuration

bootloader --location=mbr

# Clear the Master Boot Record

zerombr

# Partition clearing information

clearpart --all --initlabel

# Disk partitioning information

part swap --fstype="swap" --size=1024

part / --fstype="xfs" --grow --size=1


%post --interpreter=/bin/bash

rm -rf /etc/yum.repos.d/*

echo "

[ftp]

name=ftp

baseurl=ftp://$IP/rhel7

enabled=1

gpgcheck=0

" > /etc/yum.repos.d/ftp.repo

%end


%packages

@base


%end

' > /var/ftp/ks.cfg 

sed -i "s/\$IP/$IP/"  /var/ftp/ks.cfg 

echo -e "\e[32;1m[OK]\e[0m"


#部署default默认的菜单文件

echo -en "正在部署default默认的菜单文件......\t\t"

echo '

default vesamenu.c32

timeout 600


display boot.msg


# Clear the screen when exiting the menu, instead of leaving the menu displayed.

# For vesamenu, this means the graphical background is still displayed without

# the menu itself for as long as the screen remains in graphics mode.

menu clear

menu background splash.png

menu title PXE SERVER 

menu vshift 8

menu rows 18

menu margin 8

#menu hidden

menu helpmsgrow 15

menu tabmsgrow 13


# Border Area

menu color border * #00000000 #00000000 none


# Selected item

menu color sel 0 #ffffffff #00000000 none


# Title bar

menu color title 0 #ff7ba3d0 #00000000 none


# Press [Tab] message

menu color tabmsg 0 #ff3a6496 #00000000 none


# Unselected menu item

menu color unsel 0 #84b8ffff #00000000 none


# Selected hotkey

menu color hotsel 0 #84b8ffff #00000000 none


# Unselected hotkey

menu color hotkey 0 #ffffffff #00000000 none


# Help text

menu color help 0 #ffffffff #00000000 none


# A scrollbar of some type? Not sure.

menu color scrollbar 0 #ffffffff #ff355594 none


# Timeout msg

menu color timeout 0 #ffffffff #00000000 none

menu color timeout_msg 0 #ffffffff #00000000 none


# Command prompt text

menu color cmdmark 0 #84b8ffff #00000000 none

menu color cmdline 0 #ffffffff #00000000 none


# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.


menu tabmsg Press Tab for full configuration options on menu items.


menu separator # insert an empty line

menu separator # insert an empty line


label linux

  menu label ^Install RHEL

  kernel vmlinuz

  append initrd=initrd.img ks=ftp://$IP/ks.cfg


' > /var/lib/tftpboot/pxelinux.cfg/default

sed -i "s/\$IP/$IP/"  /var/lib/tftpboot/pxelinux.cfg/default

chmod 644 /var/lib/tftpboot/pxelinux.cfg/default


echo -e "\e[32;1m[OK]\e[0m"

echo -e "PXE服务部署成功\t\t"


具体步骤解释  请看http://blog.51cto.com/13558754/2058170



以上是关于快速搭建部署网络PXE shell 脚本的主要内容,如果未能解决你的问题,请参考以下文章

Nginx的快速部署(Centos Shell脚本)

shell脚本之一键部署PXE远程安装服务,将解放双手进行到底!

sandbox和MHA快速测试(r12笔记第32天)

shell脚本之全自动化部署PXE

快速部署Tomcat项目的Shell脚本

快速部署tomcat项目的Shell脚本