加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

第七章 if条件语句的知识和实践(邮件报警)

发布时间:2020-12-15 19:58:29 所属栏目:安全 来源:网络整理
导读:范例7-2: 开发一个脚本判断系统剩余内存的大小,如果低于100MB,就邮件报警给系统管理员,并将脚本加入系统定时任务,即每3分钟执行一次。 首先搞定邮件服务,我的centos 7上面没有mail包,所以就下载一个rpm的mail包安装,下来时编辑配置文件: [root@loca


范例7-2:

开发一个脚本判断系统剩余内存的大小,如果低于100MB,就邮件报警给系统管理员,并将脚本加入系统定时任务,即每3分钟执行一次。


首先搞定邮件服务,我的centos 7上面没有mail包,所以就下载一个rpm的mail包安装,下来时编辑配置文件:

[root@localhost~]#tail-n7/etc/mail.rc

setfrom=***@163.com
setsmtp=smtp.163.com
setsmtp-auth-user=***
setsmtp-auth-password=***
setsmtp-auth=login
[root@localhost~]#

然后测试邮件服务器是否正常:
[root@localhost~]#echo"oldboy"|mail-s"cpu"***@163.com#好了
#!/bin/bash
men=`free-m|awk'NR==3{print$NF}'`
chars="currentmemoryis$men"

if[$men-lt3000];then
echo"$chars"|tee/tmp/messages.txt#打印并且保存在文件中
mail-s"`date+%F-%T`-men"17858655319@163.com</tmp/messages.txt
fi

范例 7-3:分别使用read读入及脚本传参的方式比较两个数字的大小。把前面涉及到的是否为整数以及传参的个数是否按照要求的。

#!/bin/bash
readnum
readnum1
if[-z"$num"];then
echo"plzinputcurrtarges"
exit4
fi
if[-z"$num1"];then#发现这两个不能放一起使用-a连接起来判断,会出现问题。
echo"plzinputcurrtarges"
exit5
fi

expr$num+4>/dev/null
aa=$?
expr$num1+4>/dev/null
bb=$?

if[$aa-eq0-a$bb-eq0];then
if[$num-lt$num1];then
echo""$num"<"$num1""
else
echo""$num">"$num1""
fi
else
echo"thisisabadint"
fi



范例:监控web和数据库的企业案例

用if条件语句针对nginx Web服务或者mysql数据库服务是否正常进行检测,如果服务未启动,则启动相应的服务;

#!/bin/bash

netstat-tlnup|grep3306>/dev/null
a=$?

char="themysqlisaction"


if[$a-ne0];then
echo"themysqlisdown"
systemctlstartmariadb
echo`netstat-tlnup|grep3306`
exit5
else[$a-eq0]
echo"$char"|tee/tmp/mysql.txt
mail-s"`date+%F-%T`-mysql"17858655319@163.com</tmp/mysql.txt
fi

判断mysql最佳的方式:
[root@localhost~]#netstat-tlnup|grep3306|wc-l
1

这是本地监控,如果是远程,可以使用telnet查看远程主机的mysql的端口是不是存在。


客户端模拟用户访问的方式进行监控:

一般从web上访问mysql的地址开发会给我们,所以我们可以使用wget或者curl

之前使用wget的方式:

wget --timeout 10 --tries=2 www.baidu.com> /dev/null


总结:本机检测端口使用nc,netstat,ps,远程主机使用telnet,客户端使用wget ,curl


范例:使用if条件语句比较两个整数的大小,使用传参的方法时,需要对传参个数及传入的参数是否为整数进行判断。

#!/bin/bash

a=$1
b=$2

if[-z$1];then
echo"thisisabadidea"
exit2
fi

if[-z$2];then
echo"thisisabad"
exit3
fi

expr$a+4>/dev/null
aa=$?
expr$b+3>/dev/null
bb=$?

if[$aa-eq0-a$bb-eq0];then

if[$a-lt$b]
then
echo""$a"<"$b""
elif[$a-gt$b]
then
echo""$a">"$b""
elif[$a-eq$b]
then
echo""$a"="$b""
else
echo"plztwocurretarge"
fi
fi

范例:判断字符串是否为数字的多种思路

[root@localhost~]#[-n"`echo"helloword"|sed"s/[0-9]//g"`"]&&echochar||echoint
char
[root@localhost~]#[-n"`echo"123"|sed"s/[0-9]//g"`"]&&echochar||echoint
int
[root@localhost~]#[-z`echo"helloword"|sed"s/[0-9]//g"`]&&echoint||echochar
char
[root@localhost~]#[-z`echo"2345"|sed"s/[0-9]//g"`]&&echoint||echochar
int
[root@localhost~]#[-z"`echo${a//[0-9]/}`"]&&echoint||echochar

范例:判断字符串长度是否为0的多种思路

[root@localhost~]#[-n"$a"]&&echoyes||echono
yes
[root@localhost~]#[!-z"$a"]&&echoyes||echono
yes
[root@localhost~]#["${#a}"-eq0]&&echoyes||echono
no

范例9:开发rsync的自动脚本

解题思路是先在物理机上安装一边rsync这个软件。看如何安装,执行什么命令启动

然后给脚本加上执行权限,命名为/etc/init.d/rsyncd


#!/bin/bash

if[-z$1];then
echo"plzonecanshu"
exit5
fi

if[$1=="start"];then
rsync--daemon
["`netstat-tlnup|greprsync|wc-l`"-gt0]&&echo"startingrsgnc..."||echo"nostart"
exit6
elif[$1=="stop"];then
#kill"`ps-ef|greprsync|grep-vgrep|awk-F'''{print$2}'`"
killallrsync
sleep2
["`netstat-tlnup|greprsync|wc-l`"-eq0]&&echo"stoprsync.."||echo"nostop"
exit3
elif[$1=="restart"];then
#kill"`ps-ef|greprsync|grep-vgrep|awk-F'''{print$2}'`"
kilallrsync
sleep2
rsync--daemon
["`netstat-tlnup|greprsync|wc-l`"-gt0]&&echo"starting"||echo"stop"

exit2
else
echo"plzcurrtint"
fi

如果要实现chkconfig来管理开机自动启动

给开头加上两行:

#chkconfig: 2345 20 80

#description: Saves and restores system entropy pool

第一行是说需要chkconfig管理,2345是linux的运行级别,表示该脚本默认是在2345级别为启动状态,20是脚本的开始启动顺序,80是脚本的停止顺序,这两个数字都是不超过99的数字,一般情况下,可以根据服务的启动需求来选择,应用服务一般要考后启动为佳。

[root@localhost init.d]# vim rsyncd

[root@localhost init.d]# chkconfig --add rsyncd

[root@localhost init.d]# chkconfig --list rsyncd


注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。


要列出 systemd 服务,请执行 'systemctl list-unit-files'。

查看在具体 target 启用的服务请执行

'systemctl list-dependencies [target]'。


rsyncd 0:关 1:关 2:开 3:开 4:开 5:开 6:关

[root@localhost init.d]#

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读