shell脚本之一键部署PXE远程安装服务(含可复制代码)

PXE安装脚本

在脚本运行前配置好yum源

脚本内容如下:

  1 #!/bin/bash
  2 #一键部署PXE装机
  3 
  4 read -p "输入DHCP地址池的开始值(0-255)" num1
  5 read -p "输入DHCP地址池的结束值(0-255)" num2
  6 
  7 yum clean all   &> /dev/null
  8 yum makecache   &> /dev/null
  9 if [ $? -ne 0 ];then
 10     echo "请配置好yum源"
 11     exit 1
 12 fi
 13 
 14 #安装启用TFTP服务
 15 rpm -q tftp-server
 16 if [ $? -ne 0 ];then
 17   yum install -y tftp-server xinetd &> /dev/null
 18 else
 19    echo -e "\033[34;1m tftp-server已安装!\033[0m"
 20 fi
 21 sed -i '14s/yes/no/' /etc/xinetd.d/tftp
 22 
 23 systemctl start tftp
 24 systemctl enable tftp
 25 systemctl start xinetd
 26 systemctl enable xinetd
 27 
 28 echo -e "\033[34;1m TFTP服务成功启用!\033[0m"
 29 
 30 #安装启用DHCP服务
 31 rpm -q dhcp
 32 if [ $? -ne 0 ];then
 33      yum install -y dhcp &> /dev/null
 34 else
 35    echo -e "\033[34;1m DHCP已安装!\033[0m"
 36 fi
 37 
 38 cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
 39 
 40 ip=`cat /etc/sysconfig/network-scripts/ifcfg-ens33 | grep "IPADDR" | awk -F '=' '{print $2}'`
 41 net=`echo ${ip:0:11}`
 42 
 43 sed -i '14s/#/ /' /etc/dhcp/dhcpd.conf
 44 sed -i '14a next-server '$ip';'  /etc/dhcp/dhcpd.conf
 45 sed -i '15a filename "pxelinux.0"; '  /etc/dhcp/dhcpd.conf
 46 sed -i '34s/10.254.239/'$net'/'  /etc/dhcp/dhcpd.conf
 47 sed -i '34s/224/0/' /etc/dhcp/dhcpd.conf
 48 sed -i '35c range '$net'.'$num1' '$net'.'$num2';' /etc/dhcp/dhcpd.conf
 49 sed -i '36s/rtr-239-0-1.example.org, rtr-239-0-2.example.org/'$ip/'' /etc/dhcp/dhcpd.conf
 50 
 51 systemctl start dhcpd 
 52 if [ $? -ne 0 ];then
 53     echo -e "\033[31m DHCP开启失败,已退出!\033[0m"
 54     exit 1
 55 fi
 56 systemctl enable dhcpd
 57 
 58 #准备Linux内核、初始化镜像文件
 59 cd /mnt/images/pxeboot/
 60 cp vmlinuz /var/lib/tftpboot/ 
 61 cp initrd.img /var/lib/tftpboot/ 
 62 
 63 #准备PXE引导程序
 64 yum install -y syslinux &> /dev/null
 65 if [ $? -eq 0 ];
 66 then
 67   echo -e "\033[34;1m syslinux安装成功!\033[0m"
 68 fi
 69 cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
 70 
 71 #安装FTP服务,准备CentOS 7安装源  
 72 yum install -y vsftpd &> /dev/null
 73 
 74 if [ $? -eq 0 ];
 75 then
 76   echo -e "\033[34;1m vsftpd安装成功!\033[0m"
 77 fi
 78 
 79 mkdir /var/ftp/centos7 
 80 cp -rf /mnt/* /var/ftp/centos7/ 
 81 
 82 systemctl start vsftpd                                 
 83 systemctl enable vsftpd
 84 
 85 #配置启动菜单文件
 86 mkdir /var/lib/tftpboot/pxelinux.cfg  
 87 
 88 cat >/var/lib/tftpboot/pxelinux.cfg/default <<EOF
 89 default auto                 
 90 prompt 1                    
 91  
 92 label auto                  
 93 kernel vmlinuz              
 94 append initrd=initrd.img method=ftp://'$ip'/centos7
 95  
 96 label linux text          
 97 kernel vmlinuz
 98 append text initrd=initrd.img method=ftp://'$ip'/centos7
 99  
100 label linux rescue          
101 kernel vmlinuz
102 append rescue initrd=initrd.img method=ftp://'$ip'/centos7
103 EOF
104 
105 systemctl stop firewalld          
106 setenforce 0                    
107 
108 echo -e "\033[34;1m PXE一键装机安装成功,请前往虚拟机去测试!\033[0m"

 

运行脚本以及测试结果:

 

posted on 2021-08-16 12:03  笑洋仟  阅读(410)  评论(0编辑  收藏  举报

levels of contents