shell脚本全自动获取IP一键安装PXE

废话不多说上脚本

#!/bin/bash
#eefect:PXE自动化装机

install_pxe (){
#关闭防火墙,selinux
systemctl disable --now firewalld
setenforce 0
sed -i '7 s/enforcing/disable/' /etc/sysconfig/selinux

#安装pxe所需要的服务:tftp传输小文件,ftp传输大文件,dhcp协>议分配IP,syslinux创建新系统内核文件
yum install -y tftp-server.x86_64 vsftpd dhcp syslinux

#复制dhcp配置文件
cp -f /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf

#获取本机IP地址、网段、掩码
IP=`ifconfig |grep -m 1 netmask |awk '{print $2}'`
subnet=`route | grep ens33 |awk 'NR==2{print $1}'`
netmask=`route | grep ens33 |awk 'NR==2{print $3}'`

#修改dhcp配置文件,设置分配的IP网段
sed -i " 27 s/.*/subnet ${subnet} netmask ${netmask} {\n  range ${subnet%?}100 ${subnet%?}200;\n  option routers ${IP};\n  next-server ${IP};\n  filename \"pxelinux.0\";/ " /etc/dhcp/dhcpd.conf

#修改tftp配置文件,设置为开机自启
sed -i '14 s/yes/no/' /etc/xinetd.d/tftp

mkdir -p /var/ftp/centos7
mount /dev/sr0 /var/ftp/centos7/

#准备四大文件
cd /var/ftp/centos7/isolinux/
cp initrd.img vmlinuz /var/lib/tftpboot/
mkdir -p /var/lib/tftpboot/pxelinux.cfg
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
#编写自动化装机路径指向配置文件
cat >/var/lib/tftpboot/pxelinux.cfg/default<<EOF
default auto
prompt 0

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

label linux text
kernel vmlinuzappend text initrd=initrd.img method=ftp://${IP}/centos7

label linux rescue
kernel vmlinuzappend rescue initrd=initrd.img method=ftp://${IP}/centos7
EOF

systemctl start dhcpd
systemctl start tftp
systemctl start vsftpd

#####准备自动图形化安装ks.cfg文件#####
yum install -y system-config-kickstart.noarch

cat >/var/ftp/ks.cfg<<EOF
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext abc123
# Use network installation
url --url="ftp://${IP}/centos7"
# System language
lang zh_CN
# Firewall configuration
firewall --disabled
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
# SELinux configuration
selinux --disabled


# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Africa/Abidjan
# 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=1024
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow --size=1

%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

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
EOF
}

#####脚本主体#####

#调用pxe装机脚本
install_pxe

还有不少可优化的地方,待更新ing

posted @ 2022-08-26 17:41  玖拾一  阅读(95)  评论(0编辑  收藏  举报