linux12shell编程 -->流程if判断2

Posted FikL-09-19

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux12shell编程 -->流程if判断2相关的知识,希望对你有一定的参考价值。

流程控制之if判断

四 、练习

3、判断一个用户是否存在

[root@openvpn test]# cat check_user.sh 
#!/bin/

id $1 &> /dev/null
if [ $? -eq 0 ];then
    echo "user $1 exists"
else
    echo "user $1 not exists"
fi
[root@openvpn test]# chmod +x check_user.sh 
[root@openvpn test]# ./check_user.sh openvpn
user openvpn exists
[root@openvpn test]# ./check_user.sh xx
user xx not exists

4、检测nginx软件是否安装,没有的话则安装

[root@openvpn test]# cat check_nginx.sh
#!/bin/

rpm -q nginx &>/dev/null
if [ $? -eq 0 ];then
    echo "已经安装"
else
    echo "正在安装..."
    yum install nginx -y &>/dev/null
fi

5、判断80端口的状态,未开启则重启

[root@openvpn test]# cat check_port.sh 
#!/bin/

netstat -an |grep LISTEN |grep '\\b80\\b' &>/dev/null
if [ $? -eq 0 ];then
    echo "80端口ok"
else 
    echo "80端口down"
    echo "正在重启..."
    systemctl restart httpd &> /dev/null
    if [ $? -eq 0 ];then
        echo "重启成功"
    else
        echo "重启失败"
    fi
fi

# 检测端口
[root@openvpn day2]# cat port.sh 
# !/bin/
[ $# -ne 1 ] && echo "Usage: $0 port" && exit

netstat -an |grep -iw "listen" |grep $1 &> /dev/null


if [ $? -eq 0 ];then
    echo "nginx $1 is ok"
else
    echo "nginx $2 is down"
fi

6、编写监控脚本、配置邮箱

1)编写监控脚本
要求如果:
       根分区剩余空间小于10%
       内存的可用空间小于30%
       向用户openvpn发送告警邮件,邮件的内容包含使用率相关信息
[root@openvpn test]# cat monitor.sh 
#!/bin/

#! /bin/
# 提取根分区剩余空间
use_disk=`df / | grep / | awk '{print $5}'`
use_percent=`echo $use_disk|cut -d% -f1`

# 提取内存剩余空间
avail_mem=`free | awk 'NR==2{print $NF}'`
total_mem=`free | awk 'NR==2{print $2}'`
avail_percent=`echo "scale=2;$avail_mem/$total_mem"|bc | cut -d. -f2`

# 注意 磁盘提取的数值单位为 kb、 内存提取的单位为 Mb
if [ $use_percent -gt 90 ];then
     echo "邮件内容:根分区已经使用为${user_disk}低于10%,请及时处理!!!" | mail -s "硬盘报警邮件" root
fi

if [ $avail_percent -lt 30 ];then
     echo "邮件内容:内存剩余${free_percent}%,低于30%" | mail -s "内存报警邮件" xxx@163.com
fi

2)测试:
# 查看163邮箱
# [root@openvpn test]# cat /var/spool/mail/root 

3)mailx配置
[root@openvpn ~]# yum install mailx -y
[root@openvpn ~]# cat /etc/mail.rc
set from=378533872@qq.com 
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=378533872@qq.com
set smtp-auth-password="xxxxxxxxxx"
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
[root@openvpn day2]# cat /etc/mail.rc  #正确的
  set from=1715554782@qq.com
  set smtp=smtp.qq.com  
  set smtp-auth-user=1715554782
  set smtp-auth-password=tfdxmfopkacdbhae
  set smtp-auth=login 

解释
set from:设置发件人
set smtp:设置外部STMP服务器
set smtp-auth-user:设置STMP用户名(一般为完整邮箱地址)
set smtp-auth-password:设置SMTP密码,需要登录378533872@qq.com在设置->账户->开启POP3/SMTP服务->获取密码

测试
[root@openvpn ~]# echo "卧槽" | mail -s "报警邮件" 18611453110@163.com
[root@openvpn ~]# Error in certificate: Peer's certificate issuer has been marked as not trusted by the.

5)上述报错的解决方式为,依次执行下述命令
mkdir -p /root/.certs/

echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt

certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt

certutil -L -d /root/.certs

cd /root/.certs

certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt

# 最后出现这句就可以了
Notice: Trust flag u is set automatically if the private key is present.

# 重新修改配置文件的最后一行
[root@openvpn ~]# cat /etc/mail.rc
set from=378533872@qq.com 
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=378533872@qq.com
set smtp-auth-password="xxxxxxxxxx"
set smtp-auth=login
set ssl-verify=ignore
# set nss-config-dir=/etc/pki/nssdb/  # 改为下面一行
set nss-config-dir=/root/.certs

# 然后重新测试邮件发送即可

6)根据操作系统不同进行yum源优化 centos6 centos7 centos8
[root@openvpn shell]# cat check_yum.sh 
#!/bin/

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &>/dev/null
var=$(awk '{print $(NF-1)}' /etc/redhat-release)
os_version=`echo ${var%%.*}`
if [ $os_version -eq 7 ];then
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
elif [ $os_version -eq 6 ];then
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
elif [ $os_version -eq 5 ];then
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo &>/dev/null
else
    echo "请检查确认系统版本信息"
fi


以上是关于linux12shell编程 -->流程if判断2的主要内容,如果未能解决你的问题,请参考以下文章

Linux实战——Shell编程练习(更新12题)

Linux之Shell编程(12)--Shell输入/输出重定向实例演示

Linux之Shell编程(12)--Shell输入/输出重定向实例演示

Linux编程 12 (默认shell环境变量, PATH变量重要讲解)

linux12shell编程 --> systemctl管理脚本

linux12shell编程 -->三剑客习题汇总