Shell actual combat three cases

surroundings

  • centos 7.5
  • Mail service configuration completed

For the mail configuration, please refer to the following
to use the built-in mail to send mail under linux (super simple)

Case number one

Case goals

  • Detect the server system version number and intercept the local IP
  • Main task: Check whether the server disk usage is greater than or less than 15%
  • Whether it is greater than 15% or less than %15, you need to send emails to the qq mailbox as soon as possible
  • Significance of the case: The most important thing for our operation and maintenance engineers is to ensure the normal operation of the server. Of course, the disk is an indispensable guarantee for the normal operation of the server. When the available space of the hard disk is small to a certain extent, it will cause system swap files, Temporary files lack free space, which reduces the operating efficiency of the system, so monitoring disks has become a piece of knowledge that operation and maintenance personnel must master
#!/bin/bash
#系统版本号
os_version=$(cat /etc/redhat-release)
echo "系统版本号是:${os_version}"
#本机服务器IP地址
ip_addr=$(ifconfig ens33 |awk '/netm/{print $2}')
echo "本机服务器IP地址是:${ip_addr}"
#磁盘使用率>15%
gt_15=($(df -TH |awk -F '[ |%]+' '/xfs/{
     
     if($6>15)print $1}'))
        for i in ${gt_15[@]}
        do
                echo "disk used is gt 15%"|mail -s "disk>15%" [email protected]

        done
#磁盘使用率<15%
lt_15=($(df -TH |awk -F '[ |%]+' '/xfs/{
      
      if($6<15)print $1}'))
        for i in ${lt_15[@]}
        do
                echo "disk used is lt 15"|mail -s "disk is ok" [email protected]
        done

Case two

Case goals can refer to HTTP status codes

  • Determine the status code of a website and intercept the status code
  • Determine whether the status code is 200
  • url is the corresponding URL
  • You can use www.baidu.com to test the correct URL
  • Abnormal URL can use www.linuxpm.com
#!/bin/bash
url=(www.baidu.com www.linuxpm.com)
for url in ${url[@]}
do
        result=$(curl -I -o /dev/null -s -w %{
     
     http_code} $url)
:<<`!`
        -I:http头部信息
        -o:输出到指定位置
        -s:屏蔽输出
        -w:定制输出格式
`!`
        if [ $result -eq 200 ];then
                echo "$url is ok"|mail -s "url_健康info" [email protected]
        else
                echo "$url is error"|mail -s "url_错误info" [email protected]
        fi
done

Case three

Case goal: Prevent files from being deleted by mistake
Case map: Below the code

#!/bin/bash
while true
do
        read -ep "输入您要删除的文件:<<" file
        echo "您要删除的文件是: $file"
        read -p "您确定要删除吗?Y/y|N/n:<<" cmd1
        case $cmd1 in
                Y|y)
                        if [ -e $file ];then
                                count=$(rpm -qa |grep rsync|wc -l)
                                if [ $count -ne 0 ];then
                                        echo "rsync已经安装"
                                else
                                        echo "rsync未安装,即将安装"
                                        yum -y install rsync &> /dev/null
                                        echo "rsync安装完成"
                                fi
                                        echo "判断服务rsync是否运行"
                                        netstat -nltp|grep 873 &> /dev/null
                                if [ $? -eq 0 ];then
                                        echo "rsync服务已运行"
                                else
                                        echo "rsync服务未运行,即将运行"
                                        systemctl start rsyncd &> /dev/null
                                        echo "rsync服务运行成功"
                                fi
                                #开始备份文件
                                backup_dir="/usr/local/src/backup"
                                [ -d ${backup_dir} ] || mkdir ${backup_dir}
        case $cmd1 in
                Y|y)
                        if [ -e $file ];then
                                count=$(rpm -qa |grep rsync|wc -l)
                                if [ $count -ne 0 ];then
                                        echo "rsync已经安装"
                                else
                                        echo "rsync未安装,即将安装"
                                        yum -y install rsync &> /dev/null
                                        echo "rsync安装完成"
                                fi
                                        echo "判断服务rsync是否运行"
                                        netstat -nltp|grep 873 &> /dev/null
                                if [ $? -eq 0 ];then
                                        echo "rsync服务已运行"
                                else
                                        echo "rsync服务未运行,即将运行"
                                        systemctl start rsyncd &> /dev/null
                                        echo "rsync服务运行成功"
                                fi
                                #开始备份文件
                                backup_dir="/usr/local/src/backup"
                                [ -d ${backup_dir} ] || mkdir ${backup_dir}
                                #开始执行备份操作
                                rsync -av $file ${backup_dir}
                                echo "input Y:<< delete"
                                echo "input y:<< no delete"
                                read -p "您真的狠心删除我吗?(Y|y):<<" cmd2
                                case $cmd2 in
                                        Y)
                                                echo "start delete $file"
                                                rm -rf $file
                                                echo "文件恢复路径:<< ${backup_dir}"
                                                ;;
                                        y)
                                                echo "no delete"
                                                echo "就知道您不会抛弃我的~_~"
                                                exit
                                                ;;
                                esac
                        else
                                echo "start touch $file"
                                touch $file
                                echo "show $file"
                                ls -l $file
                                exit
                        fi
                        ;;
                N|n)
                        echo "no delete $file"
                        echo "退出"
                        exit
                        ;;
        esac
done

This is the end of the three cases of mind map shell to prevent accidental deletion of files
Insert picture description here
~ _ ~

Guess you like

Origin blog.csdn.net/qq_49296785/article/details/108705480