第6章 综合实战
一、实战1:判断主机存活三次机会脚本内容:
#!/usr/bin/bash
#ip_list="10.18.40.1 10.18.42.127 10.18.42.8 10.18.42.5"
while read ip
do
for count in {1..3}
do
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip ping is ok."
break
else
echo "$ip ping is failure: $count"
fail_count[$count]=$ip
fi
done
if [ ${#fail_count[*]} -eq 3 ] ;then
echo "${fail_count[1]} ping is failure!"
unset fail_count[*]
fi
done <ip.txt
二、实战2:布置discuz论坛脚本内容:
#!/bin/bash
#stop selinux and firewalld
echo "work is starting!"
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
setenforce 0 &> /dev/null
systemctl stop firewalld.service
systemctl disable firewalld.service
#LAMP
if ! rpm -q httpd &> /dev/null ;then
echo "LAMP开始布置..."
yum -y install httpd mariadb-server mariadb php php-mysql gd php-gd &> /dev/null
fi
echo "LAMP已经布置完成!"
#start httpd mariadb
systemctl start httpd mariadb &> /dev/null
systemctl enable httpd mariadb &> /dev/null
echo "httpd mariadb 启动完成!"
#discuz
discuz=Discuz_X2.5_SC_UTF8.zip
if ! rpm -q wget &>/dev/null ;then
yum -y install wget &> /dev/null
fi
if [ ! -f $discuz ] ;then
wget http://download.comsenz.com/DiscuzX/2.5/$discuz &>/dev/null
fi
if [ ! -f /webroot/discuz ] ;then
mkdir -p /webroot/discuz
fi
if ! rpm -q unzip &>/dev/null ;then
yum -y install unzip &> /dev/null
fi
if [ ! -d upload ] ;then
unzip $discuz &> /dev/null
fi
cp -rf upload/* /webroot/discuz/
chown -R apache.apache /webroot/discuz/
echo "导入discuz网站源码完成!"
#Apache 配置虚拟主机
if [ ! -f /etc/httpd/conf.d/discuz.conf ];then
cat >> /etc/httpd/conf.d/discuz.conf <<-EOF
<VirtualHost *:80>
ServerName www.discuz.com
DocumentRoot /webroot/discuz
</VirtualHost>
<Directory "/webroot/discuz">
Require all granted
</Directory>
EOF
fi
systemctl restart httpd
echo "虚拟主机配置完成!"
#准备数据库
mysql -e "use discuz" &> /dev/null
if [ $? -ne 0 ] ;then
mysql -e "create database discuz"
fi
echo "数据库准备完成!"
#请安装discuz
ips=`ifconfig |grep inet | awk 'NR==1{print $2}'`
echo "请在浏览器中输入http://$ips "