KICKSTART无人值守安装系统shell2

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH

dhcp_conf="/etc/dhcp/dhcpd.conf"
#dhcp配置文件
dhcpd_service_old="/usr/lib/systemd/system/dhcpd.service"
#原dhcp服务文件
dhcpd_service="/etc/systemd/system/dhcpd.service"
#dhcp服务文件
tftp_conf="/etc/xinetd.d/tftp"
#tftp配置文件
http_default_dir="/var/www/html"
#http默认发布目录
http_dir="KaTeX parse error: Expected 'EOF', got '#' at position 39: …os-7" #̲http发布目录:/var/w…{tftpboot}/pxelinux.cfg/default"
#default文件:/var/lib/tftpboot/pxelinux.cfg/default

function ip_configure(){
#网络配置
echo “Whether the network is configured?”
select var in “Yes” “No”;do
if [ “ v a r " = = " N o " ] ; t h e n i p l i n k s e t d e v e n s 37 u p i p a d d r a d d 10.100.10.1 / 24 d e v e n s 37 s y s t e m c t l r e s t a r t n e t w o r k b r e a k e l i f [ " {var}" == "No" ];then ip link set dev ens37 up ip addr add 10.100.10.1/24 dev ens37 systemctl restart network break elif [ " {var}” == “Yes” ];then
break
else
echo “Input error,please retype.”
fi
done
}
ip_configure

function dhcp_install(){
#安装DHCP(select询问)
echo “Has DHCP been installed?”
select var in “Yes” “No”;do
if [ “ v a r " = = " N o " ] ; t h e n y u m y i n s t a l l d h c p b r e a k e l i f [ " {var}" == "No" ];then yum -y install dhcp break elif [ " {var}” == “Yes” ];then
break
else
echo “Input error,please retype.”
fi
done
}
dhcp_install

function dhcp_install2(){
#安装DHCP(自动检测)
#(和select询问函数不同时调用)
rpm -qa | grep dhcp >> /tmp/test.txt
[ $? -eq 0 ] || yum -y install dhcp
}
#dhcp_install2

function dhcp_configure(){
#修改DHCP配置文件
test -e ${dhcp_conf} && mv ${dhcp_conf} KaTeX parse error: Expected 'EOF', got '#' at position 20: …p_conf}_backup #̲若{dhcp_conf}存在就将其备份
cat>${dhcp_conf}<<EOF
subnet 10.100.10.0 netmask 255.255.255.0 {
range 10.100.10.10 10.100.10.254;

可分配的起始IP-结束IP

option subnet-mask 255.255.255.0;

设定netmask

default-lease-time 21600;

设置默认的IP租用期限

max-lease-time 43200;

设置最大的IP租用期限

next-server 10.100.10.1;

告知客户端TFTP服务器的ip

filename “/pxelinux.0”;

告知客户端从TFTP根目录下载pxelinux.0文件

#option domain-name-servers www.baidu.com;
#option domain-name “10.100.10.1”;
#option routers 10.100.10.1;
#option broadcast-address 10.100.10.255;
}
EOF

#指定监听网卡
cp ${dhcpd_service_old} ${dhcpd_service}
sed -i ‘s/dhcpd --no-pid/dhcpd --no-pid ens37/g’ ${dhcpd_service}
#指定监听网卡
#sed -i ‘/dhcpd --no-pid/a\ens37’
#在下一行添加
systemctl --system daemon-reload
systemctl restart dhcpd.service
}
dhcp_configure

function tftp_install(){
#安装TFTP(select询问)
echo “Has TFTP been installed?”
select var in “Yes” “No”;do
if [ “ v a r &quot; = = &quot; N o &quot; ] ; t h e n y u m y i n s t a l l x i n e t d t f t p s e r v e r b r e a k e l i f [ &quot; {var}&quot; == &quot;No&quot; ];then yum -y install xinetd tftp-server break elif [ &quot; {var}” == “Yes” ];then
break
else
echo “Input error,please retype.”
fi
done
}
tftp_install

function tftp_install2(){
#安装TFTP(自动检测)
#(和select询问函数不同时调用)
rpm -qa | grep xinetd >> /tmp/test.txt
[ $? -eq 0 ] || yum -y install xinetd
rpm -qa | grep tftp-server >> /tmp/test.txt
[ $? -eq 0 ] || yum -y install tftp-server
}
#tftp_install2

function tftp_configure(){
#修改TFTP配置文件
cp ${tftp_conf} ${tftp_conf}_backup
sed -i ‘s/disable = yes/disable = no/g’ ${tftp_conf}
#将disable改为no
systemctl restart xinetd
systemctl restart tftp.socket
systemctl restart tftp.service
}
tftp_configure

function http_install(){
#安装HTTP(select询问)
echo “Has HTTP been installed?”
select var in “Yes” “No”;do
if [ “ v a r &quot; = = &quot; N o &quot; ] ; t h e n y u m y i n s t a l l h t t p d b r e a k e l i f [ &quot; {var}&quot; == &quot;No&quot; ];then yum -y install httpd break elif [ &quot; {var}” == “Yes” ];then
break
else
echo “Input error,please retype.”
fi
done
}
http_install

function http_install2(){
#安装HTTP(自动检测)
#(和select询问函数不同时调用)
rpm -qa | grep httpd >> /tmp/test.txt
[ $? -eq 0 ] || yum -y install httpd
}
#http_install2

function http_configure(){
#修改HTTP配置文件
cd /etc/httpd/conf.d/
test -e welcome.conf && mv welcome.conf welcome.conf_backup
#若welcome.conf存在就将其备份
test -d ${http_dir} || mkdir ${http_dir}
#若welcome.conf不存在就建立目录
df -h | grep /dev/sr0 > /tmp/test.txt
[ $? -eq 0 ] || mount /dev/cdrom ${http_dir}
#如果没有挂载光盘则挂载
}
http_configure

function syslinux_install(){
#安装syslinux(select询问)
echo “Has syslinux been installed?”
select var in “Yes” “No”;do
if [ “ v a r &quot; = = &quot; N o &quot; ] ; t h e n y u m y i n s t a l l s y s l i n u x b r e a k e l i f [ &quot; {var}&quot; == &quot;No&quot; ];then yum -y install syslinux break elif [ &quot; {var}” == “Yes” ];then
break
else
echo “Input error,please retype.”
fi
done
}
syslinux_install

function syslinux_install2(){
#安装syslinux(自动检测)
#(和select询问函数不同时调用)
rpm -qa | grep syslinux >> /tmp/test.txt
[ $? -eq 0 ] || yum -y install syslinux
}
#syslinux_install2

function pxe_configure(){
#配置支持PXE的启动程序
cp /usr/share/syslinux/pxelinux.0 ${tftpboot}
#复制启动菜单程序文件
cp -a ${http_dir}/isolinux/* ${tftpboot}
test -d ${tftpboot}/pxelinux.cfg || mkdir -p KaTeX parse error: Expected 'EOF', got '#' at position 25: …}/pxelinux.cfg #̲若{tftpboot}/pxelinux.cfg不存在就建立目录
#新建一个pxelinux.cfg目录,存放客户端的配置文件
cp ${http_dir}/isolinux/isolinux.cfg ${default}
}
pxe_configure

function default_configure(){
#整合编辑default配置文件
test -e ${default} && mv ${default} KaTeX parse error: Expected 'EOF', got '#' at position 18: …efault}_backup #̲若{default}存在就将其备份
cat>${default}<<EOF
default ks
prompt 0
label ks
kernel vmlinuz
append initrd=initrd.img ks=http://10.100.10.1/ks_config/centos7-ks.cfg
#告诉安装程序ks.cfg文件在哪里
#append initrd=initrd.img ks=http://10.100.10.1/ks_config/centos7-ks.cfg ksdevice=ens37
#ksdevice=eth0代表当客户端有多块网卡的时候,要实现自动化需要设置从eth1安装,不指定的话,安装的时候系统会让你选择,那就不叫全自动化了。
EOF
}
default_configure

function ks_cfg_touch(){
#创建ks.cfg文件
cd /tmp
cat>/tmp/centos7-ks.cfg<<EOF
auth --enableshadow --passalgo=sha512
url --url=“http://10.100.10.1/centos-7/
text
keyboard --vckeymap=cn --xlayouts=‘cn’
lang zh_CN.UTF-8

network --bootproto=dhcp --device=ens33 --onboot=yes --ipv6=auto --no-activate
network --hostname=localhost.localdomain

rootpw --iscrypted 6 6 X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
services --enabled=“chronyd”
timezone --utc Asia/Shanghai
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
autopart --type=lvm
clearpart --none --initlabel

%packages
@^minimal
@core
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

#将cfg文件放置apache的发布目录下,使其让client通过WEB能够访问:
test -d ${http_default_dir}/ks_config || mkdir KaTeX parse error: Expected 'EOF', got '#' at position 30: …dir}/ks_config #̲若{http_default_dir}/ks_config不存在就建立目录
test -e /tmp/centos7-ks.cfg && mv /tmp/centos7-ks.cfg ${http_default_dir}/ks_config
#若/tmp/centos7-ks.cfg存在就将其移动
}
ks_cfg_touch

select语句
select 表达式是一种bash的扩展应用,擅长于交互式场合。用户可以从一组不同的值中进行选择。自动用1,2,3,4列出菜单 (没有echo指令,自动显示菜单)自动read输入选择 (没有 read指令,自动输入)赋值给变量 (没有赋值指令,自动输入数字后,赋值字符串给变量)
echo “What is your favourite OS?”
select var in “Linux” “Gnu Hurd” “Free BSD” “Other”; do
break; #select本身就是一个循环,break是当选择后,就跳出循环
done
echo “You have selected v a r &quot; t e s t e f f i l e d d i r e c t o r y t e s t i n g = var&quot; test指令测试 -e:该档名是否存在 -f:该档名是否存在且为档案(file) -d:该文件名是否存在且为目录(directory) testing= (netstat -tuln | grep “:80 “) # 侦测看 port 80 在否?
if [ “$testing” != “” ]; then
echo “WWW is running in your system.”
fi
命令行参数
执行的脚本档名为 $0 这个发量,第一个接的参数就是 $1
$# :代表后接的参数[个数];
$@ :代表[”$1” “$2” “$3” “$4” ]之意,参数的全部内容,每个变量是独立的(用双引号括起来);
$* :代表 [”$1c$2c$3c$4"]其中 c 为分隔字符,默认为空格键;
设置参数的默认值echo ${1:-默认值}
Shell变量的初始值赋值
${var:-默认值} 只是临时调用,变量var并不改变,仍然为空;
${var:=默认值} 调用初始值后,变量var也赋予了一个值。
${vari:?value} 当vari没有定义时,或者定义了值为空,将在终端报错并且退出,用于检查是 否定义以及是否为空
${vari:+value} 当vari定义并且不为空,将用value替换vari的值,否则什么也不做, 与 ${vari:-value}相反
echo $?
可以看到上一条命令的返回值。linux的命令一般在正确执行后会返回0,执行失败的返回值都不一样。
可以直接在命令后用||,&&等符号,当命令执行成功时,||后的命令不会执行,
当命令失败时,&&后的命令不会执行。
if [ KaTeX parse error: Expected 'EOF', got '\a' at position 63: …不会将它当成一般文字输出: \̲a̲ ̲发出警告声; \b 删除前一…{fileuser:-“filename”}
fileuser 变量未设定或者为空,filename取"-“后面的值;否则filename= f i l e u s e r f i l e n a m e = fileuser filename= {fileuser-“filename”}:
fileuser 变量未设定,filename取”-"后面的值;否则如果变量fileuser为空或者有值 filename=$fileuser
centos查看是否安装了某个软件
1. rpm包安装的,可以用rpm -qa看到,如果要查找某软件包是否安装,用 rpm -qa | grep “软件或者包的名字”。
2. deb包安装的,可以用dpkg -l能看到。如果是查找指定软件包,用dpkg -l | grep “软件或者包的名字”; 
3. yum方法安装的,可以用yum list installed查找,如果是查找指定包,命令后加 | grep “软件名或者包名”; 
        例如:yum list installed openssl |grep openssl
                openssl.x86_64                     1:1.0.2k-8.el7                      @anaconda
        如果没有返回值则表示没有安装
4. 如果是以源码包自己编译安装的,例如.tar.gz或者tar.bz2形式的,这个只能看可执行文件是否存在了, 
上面两种方法都看不到这种源码形式安装的包。如果是以root用户安装的,可执行程序通常都在/sbin:/usr/bin目录下
5. pip安装的所有包:
pip list
比较-eq和=
-eq 用于 INTEGER 类型比较。
= 既适用于 STRING 类型比较,又适用于 INTEGER 类型比较。

猜你喜欢

转载自blog.csdn.net/qq_41906344/article/details/82948052