Linux SH脚本系列 | PXE无人值守自动应答 | 超详细

#/bin/bash
echo -e "\033[5m***检查当前网络模式***\033[0m"
ping -c 3 -i 0.5 -w 2 www.baidu.com &> /dev/null
if [ $? -eq 0 ] 
then
    echo -e "\033[1;31;5m 当前为联网状态 \033[0m"
    echo -e "\033[1;31;5m 配置在线源 \033[0m"
    cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
	wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    yum clean all 
    yum makecache
	yum -y install epel-release
	wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	yum list
    echo -e "/033[1;31;5m yum在线配置完成 \033[0m"
else
    echo -e "\033[46m 当前为单机状态 \033[0m"
	echo -e "\033[1;31;5m 配置本地源 \033[0m"	
    mount /dev/cdrom /mnt/       
    cd /etc/yum.repos.d/      
    mkdir repos.bak          
    mv *.repo ./repos.bak     
    cd /etc/yum.repos.d/         
	echo -e "      
    [local]                    
    name=local            
    baseurl=file:///mnt   
    enabled=1                  
    gpgcheck=0   " > /etc/repos.d/local.repo              
    yum clean all 
    yum makecache  
fi

myip=$(ifconfig | awk 'NR==2{print $2}')
mymask=$(ifconfig | awk 'NR==2{print $4}')
mygateway=$(ip route show | awk 'NR==1{print $3}')
echo "**********安装PXE相关服务***************************"
yum -y install tftp-server xinetd;
echo "**********修改PXE相关配置文件***********************"
sed -i '10c wait=yes' /etc/xinetd.d/tftp;
sed -i '14c disable=yes' /etc/xinetd.d/tftp;
echo "**********开启PXE相关服务***********************"
systemctl start tftp;
systemctl enable tftp;
systemctl start xinetd; 
systemctl enable xinetd;
echo "**********安装DHCP相关服务***************************"
yum -y install dhcp;
echo "*********复制DHCP模板********************************"
cp -rf /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf;
echo "*********写入DHCP服务器文件***************************"
ifconfig
read -p "分配地址的网段:" num2
read -p "分配地址的开始范围:" num
read -p "分配地址的结束范围:" num1
sed -i "32c subnet $num2 netmask $mymask {" /etc/dhcp/dhcpd.conf;
sed -i "33c  range $num $num1;" /etc/dhcp/dhcpd.conf;
sed -i "34c  option routers $myip;" /etc/dhcp/dhcpd.conf;
sed -i "35c }" /etc/dhcp/dhcpd.conf;
echo "
next-server $myip;
filename \"pxelinux.0\";
ddns-update-style none;" >> /etc/dhcp/dhcpd.conf;
echo "*********开启DHCP服务********************************"
systemctl start dhcpd;
systemctl enable dhcpd;
echo "************挂载光盘镜像******************************************"
mount /dev/sr0 /mnt;
cd /mnt/images/pxeboot;
cp vmlinuz /var/lib/tftpboot/;
cp initrd.img /var/lib/tftpboot/;
yum -y install syslinux;
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/;
yum -y install vsftpd;
mkdir /var/ftp/centos7;
cp -rf /mnt/* /var/ftp/centos7/;
systemctl start vsftpd;
systemctl enable vsftpd; 
mkdir /var/lib/tftpboot/pxelinux.cfg;
echo"*************写入内核引导文件************************"
echo "
default auto
prompt 0

label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://$myip/centos7 ks=ftp://$myip/ks.cfg

label linux text
kernel vmlinuz
append initrd=initrd.img method=ftp://$myip/centos7

label linux rescue
kernel vmlinuz
append initrd=initrd.img method=ftp://$myip/centos7" > /var/lib/tftpboot/pxelinux.cfg/default;

echo "**************************启动相关服务***************************"
systemctl stop firewalld;
setenforce 0;
systemctl restart xinetd;
systemctl restart tftp;
systemctl restart dhcpd;
systemctl restart vsftpd;
systemctl restart network;

echo "**************************安装PXE***************************"
yum install -y system-config-kickstart;
system-config-kickstart;



echo -e "
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext 1
# Use network installation
url --url=\"ftp://${myip}/centos7\"
# System language
lang zh_CN
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled

# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype=\"xfs\" --size=500
part swap --fstype=\"swap\" --size=4096
part / --fstype=\"xfs\" --grow --size=1

%post --interpreter=/bin/bash
mkdir /etc/yum.repos.d/repo.bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak
echo \"
[local]
name=local
baseurl=ftp://mnt
enabled=1
gpgcheck=0 \" > /etc/yum.repos.d/local.repo

%end

%packages
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
chrony
kexec-tools

%end " > /root/ks.cfg

cp -rf /root/ks.cfg /var/ftp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值