Shell脚本编程学习笔记(十二)---- 案例集
本文已参与[新人创作礼]活动,一起开启掘金创作之路。
案例集收藏的是一些案例联系,可以自己尝试着做做!
案例1
主机存活状态(判断3次)
#!/usr/bin/bash
IP=192.168.200
for i in {1..254}
do
ip=$IP.$i
for i in {1..3} #ping3次
do
{
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo $ip 连接成功!
break
fi
echo $ip 尝试"$i"次失败!
}&
done
done
案例2
Mysql部署
#!/usr/bin/bash
#1.检查是否存在Mysql对应的软件包
rpm_install_mysql(){
rpm_check_mysql=$(rpm -qa|grep mysql-communit-server|wc -l)
rpm_check_mysql_version=$(rpm -qa|grep mysql-communit-server)
if [ $rpm_check_mysql -eq 0 ];then
cat >/etc/yum.repos.d/mysql.repo<<-EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
EOF
yum clean all && yum makecache
yum install -y mysql-community-server
else
echo "${rpm_check_mysql_version%-*} install is OK!"
fi
}
#2.初始化MySQL数据库
clear_mysql(){
systemctl start mysqld
if [ $? -eq 0 ];then
Mysql_old_pass=$(grep "temporary password" /var/log/mysql.log |tail -n1|awk -F ' :' '{print $NF}')
mysqladmin -uroot -p"$Mysql_old_pass" password "CatGod007.com"
fi
}
#3.函数调用
rpm_install_mysql()
clear_mysql()
案例3
服务器初始化脚本
#!/usr/bin/bash
#服务器初始化脚本
Rep(){
#1.配置yum源
yum install -y wget #安装下载工具,防止yum源删除后无法使用
rm -rf /etc/yum.repos.d/*.repo
wget -O /etc/yum.repos.d/Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache
#2.安装基础软件,更新内核
yum install vim nc iotop iftop glances telnet -y
#um update && rm -rf /etc/yum.repos.d/CentOS*
}
Limit(){
#3.调整文件描述符
cat > /etc/security/limits.d/20-nproc.conf<<-EOF
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
EOF
}
Mood(){
#4.调整时区
test -f /etc/localtime && rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#5.调整语言
sed -i 's#LANG=.*#LANG="en_US.UTF-8"#g' /etc/locale.conf
#6.关闭ipv6
cd /etc/modprobe.d/ && touch ipv6.conf
cat > /etc/modprobe.d/ipv6.conf <<-EOF
alias net-pf-10 off
alias ipv6 off
EOF
#7.调整历史命令
sed -i '/HISTSIZE=/cHISTSIZE=100000' /etc/profile
grep -q 'HISTTIMEFORMAT' /etc/profile
if [ $? -eq 0 ];then
sed -i 's/^HISTTIMEFORMAT=.*$/HISTTIMEFORMAT="%F %T"/' /etc/profile
else
echo 'HISTTIMEFORMAT="%F %T"' >> /etc/profile
fi
}
#8.墙与selinux
Firewalld(){
systemctl stop firewalld
systemctl disable firewalld
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
}
#9.调整内核参数
Krnel(){
cat > /etc/sysctl.conf <<-EOF
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=0
EOF
}
#函数调用
Rep
Limit
Mood
Firewalld
Krnel
改进措施:判断,输出
案例4
主控脚本(动态扩展)
vi montor_main.sh
#!/usr/bin/bash
resettem=$(tput sgr0)
declare -A ssharray
i=0
numbers=""
for scripts_file in $(ls -I "montor_main.sh" ./)
do
echo -e "\e[1;34m" "The Scripts:" ${i} '==>' ${resettem} ${scripts_file}
grep -E "^#Program function" ${scripts_file}
ssharray[$i]=${scripts_file} #关联数组
numbers="${numbers} | ${i}" #拼接
i=$((i+1))
done
while true
do
read -p "Please input a number [ ${number} ]:" execshell
if [[ ! ${execshell} =~ ^[0-9]+ ]];then
exit
fi
/usr/bin/bash ./${ssharray[$execshell]}
done
案例5
shell MySQL的主从同步
分析:判断端口是否能正常连接;登陆从库进行状态查看;提取状态信息进行比对;比对mysql主从同步延迟
#!/usr/bin/bash
IP=192.168.200
User=root
Pass=CatGod007.com
Sync_Date=$(date +%F)
for i in {1..254}
do
IP_MySQL=$IP.$i
echo "##正在操作$IP_MySQL"
/usr/bin/mysql -h$IP_MySQL -u$User -p$Pass -e "show slave status\G" 2>/dev/null |egrep -i 'Slave_IO_Running:|Slave_SQL_Running:' &>/tmp/status.txt
if [ ! -s /tmp/status.txt ];then
echo "当前MySQL是主库“
break
fi
IO=$(grep "IO" /tmp/status.txt |awk '{print $2}')
SQL=$(grep "SQL" /tmp/status.txt |awk '{print $2}')
if [[ $IO == "yes" && $SQL == "yes" ]];then
echo "当前MySQL主从状态OK!"
else
#mail -s "主从同步失败$IP_MySQL" </tmp/status.txt
/usr/bin/mail -s "${Sync_Date}-${IP_MySQL}主从同步失败" 2337563298@qq.com <"/tmp/status.txt"
echo "当前Slave的IO状态是: $IO"
echo "当前Slave的SQL状态是: $SQL"
fi
done
案例6
应用日志分析
实现的功能如下
一:分析HTTP状态码在100-200,200-300,300-400,400-500,500以上,五个区间的请求条数。
#!/usr/bin/bash
Logfile=/soft/scripts/log/log.bjstack.log #日志位置
#状态码
awk '{if($9>=100 && $9<200){i++}
else if ($9>=200 && $9<300){j++}
else if ($9>=300 && $9<400){k++}
else if ($9>=400 && $9<500){n++}
else if ($9>=500){p++}}
END{print i,j,k,n,p,i+j+k+n+p}' $Logfile
进化版如下
#!/usr/bin/bash
resettem=$(tput sgr0)
Logfile=/soft/scripts/log/log.bjstack.log #日志位置
Nginx_Sataus_code=(`cat $Logfile |egrep -io "HTTP/1.[0|1]"[[:blank:]][0-9]{3}}
#也可以写成下面这样
#Nginx_Sataus_code=( $(cat $Logfile |egrep -io "HTTP/1.[0|1]"[[:blank:]][0-9]{3}}
"|awk -F "[ ]+" '{
if($2>=100 && $2<200)
{i++}
else if($2>=200 && $2<300)
{j++}
else if($2>=300 && $2<400)
{k++}
else if($2>=400 && $2<500)
{n++}
else if($2>=500)
{p++}
}END{print i?i:0,j?j:0,k?k:0,n?n:0,p?p:0,i+j+k+n+p}' #如没有的话,就默认0
`)
#也可以写成下面这样
#))
echo -e "\e[1;35m" "Http Status[100+]: ""${resettem} ${Nginx_Sataus_code[0]}"
echo -e "\e[1;35m" "Http Status[200+]: ""${resettem} ${Nginx_Sataus_code[1]}"
echo -e "\e[1;35m" "Http Status[300+]: ""${resettem} ${Nginx_Sataus_code[2]}"
echo -e "\e[1;35m" "Http Status[400+]: ""${resettem} ${Nginx_Sataus_code[3]}"
echo -e "\e[1;35m" "Http Status[500+]: ""${resettem} ${Nginx_Sataus_code[4]}"
echo -e "\e[1;35m" "Http Status[Total]: ""${resettem} ${Nginx_Sataus_code[5]}"
感谢大家,点赞,收藏,关注,评论!
浙公网安备 33010602011771号