1、编写监控脚本,如果
根分区剩余空间小于10%
内存的可用空间小于30%
发送告警邮件,邮件的内容包含使用率相关信息(用echo命令暂时代替发邮件)
shell
#!/bin/bash
# 提取根分区剩余空间
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%,请及时处理!!!"
fi
if [ $avail_percent -lt 30 ];then
echo "邮件内容:内存剩余${free_percent}%,低于30%"
fi
2、将当日访问次数超过800次的ip地址禁掉
结合计划任务每3分钟执行一次(写出计划任务的时间+命令)
shell
*/3 * * * * /scripts/ban_ip.sh
#!/bin/bash
# 统计当天每个 IP 的访问次数
current_day_count_ip=$(cat /var/log/nginx/access.log \
| grep `LANG="en_US.UTF-8" && date +"%d/%b/%Y"` \
| awk '{print $1}' \
| sort \
| uniq -c \
| sort -rn \
| awk '{print $1":"$2}')
# 遍历每个 IP 的访问次数
for info in $current_day_count_ip; do
count=`echo $info | cut -d: -f1` # 访问次数
if [ $count -lt 800 ]; then # 小于阈值的跳过
continue
fi
IP=`echo $info | cut -d: -f2` # IP 地址
# 检查 iptables 是否已存在该规则
/sbin/iptables -t filter -L -n | grep $IP
if [ $? -eq 0 ]; then
echo "IP:$IP 已禁用过的,无需重复" >> /tmp/banip.log
continue
fi
# 记录日志并封禁 IP
echo "$(date '+%Y-%m-%d_%H:%M:%S') IP:$IP is over $count, BAN IT" >> /tmp/banip.log
/sbin/iptables -I INPUT -s $IP -j DROP
done
3、检测ip脚本:./ping.sh 1.1.1.11 可以返回成功or失败两个结果(5分钟)
shell
#!/bin/bash
# 检查是否传入 IP
if [ -z "$1" ]; then
echo "Usage: $0 <IP>"
exit 1
fi
IP=$1
# ping 一次,超时 5 秒
if ping -c 1 -W 5 "$IP" &> /dev/null; then
echo "成功"
else
echo "失败"
fi
4、并发检测某个网段所有ip的脚本(5分钟)
shell
#!/bin/bash
for i in {1..254}
do
(
ip_addr=192.168.71.$i
ping -c1 $ip_addr &>/dev/null
if [ $? -eq 0 ];then
echo "$ip_addr is ok"
else
echo "$ip_addr is no"
fi
) &
done
5、编写脚本,可以计算任意两个数的+、-、*、/的结果,要求输入的位置参数必须为2个(5分钟)
shell
#!/bin/bash
a=$1
b=$2
[ $# -ne 2 ] && {
echo "请输入两位数字信息"
exit
}
echo "a-b=$((a-b))"
echo "a+b=$((a+b))"
echo "a*b=$((a*b))"
echo "a/b=$((a/b))"
6、编写脚本,./sum.sh 111 222 333 444 # 传入任意个数的整数,脚本都能求出他们的和(5分钟)
shell
#!/bin/bash
res=0
for i in $*
do
((res+=i))
done
echo $res
7、用while循环与for循环两种方式打印从1到5,跳过3 (5分钟)
shell
#!/bin/bash
i=1
while [ $i -lt 6 ]
do
if [ $i -eq 3 ];then
let i++
continue
fi
echo $i
let i++
done
8、编写跳板机程序,把配置信息存入配置文件。脚本执行时用户可以选择标号登录到目标主机(10分钟)
shell
0、BACKUP 1.1.1.11
1、WEB01 1.1.1.12
2、WEB02 1.1.1.14
3、DB01 192.168.71.4
[root@www.egonlin.com /work]# cat jump.sh
#!/bin/bash
#ip_array=([1]="1.1.1.11" [2]="1.1.1.12" [3]="1.1.1.14" [4]="192.168.71.4")
ip_array=(`cat conf/jump.conf |awk '{print $NF}'`)
while true
do
cat ./conf/jump.conf
read -p "请输入编号:" num
case $num in
0)
echo ${ip_array[$num]}
;;
1)
echo ${ip_array[$num]}
;;
2)
echo ${ip_array[$num]}
;;
3)
ssh root@${ip_array[$num]}
;;
quit)
break
;;
*)
echo "编号输入错误"
esac
done
echo "跳板机终止"
9、写出成绩等级查询脚本(5分钟)
>90 分优秀
80分-90区间良好
70-80区间一般
其他情况不及格
shell
read -p "输入自己的分数: " score
if [[ "$score" =~ ^[0-9]+$ ]];then
if [ $score -ge 90 ];then
echo "优秀"
elif [ $score -ge 80 ];then
echo "良好"
elif [ $score -ge 70 ];then
echo "一般"
else
echo "很差"
fi
else
echo "输入的score必须是数字"
fi
10、编写登录认证程序,最多输错三次密码则退出(10分钟)
shell
#!/bin/bash
username="egon"
password="123"
failed=0
while true
do
read -p "input your username: " inp_user
read -p "input your password: " inp_pwd
if [ "$inp_user" = "$username" ] && [ "$inp_pwd" = "$password" ];then
echo "login successfull"
break
else
echo "username or password error"
let failed++
fi
if [ $failed -eq 3 ];then
echo "输错的次数太多了,退出"
break
fi
done
11、编写检测url连通性程序,url访问失败三次则判断为失败,访问成功一次则判定为成功(10分钟)
shell
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage: $0 url"
exit
fi
failed=0
while true
do
code=`curl -I -s $1 | awk 'NR==1{print $2}'`
if [ "$code" = "200" ];then
echo "$1 is ok"
break
else
let failed++
echo "=============>$failed"
fi
if [ $failed -eq 3 ];then
echo "$1 is error"
break
fi
done
12、编写脚本,命令行传入一个目录,统计出该目录下目录文件、普通文件、软连接文件、其他文件的个数(5分钟)
shell
#!/bin/bash
check_dir=$1
x=0
y=0
z=0
a=0
for filename in `ls $check_dir`
do
file_path="$check_dir/$filename"
if [ -d $file_path ];then
let x++
elif [ -f $file_path ];then
let y++
elif [ -L $file_path ];then
let z++
else
let a++
fi
done
echo "目录文件个数:$x"
echo "普通文件个数:$y"
echo "连接文件个数:$z"
echo "其他文件个数:$a"
13、写脚本,命令行可以传入任意数量的文件路径,脚本处理后输出每个文件的文件类型(5分钟)
shell
#!/bin/bash
for filename in "$@"
do
if [ -d $filename ];then
echo "$filename 是一个目录"
elif [ -f $filename ];then
echo "$filename 是一个普通文件"
elif [ -b $filename ];then
echo "$filename 是一个块设备"
else
echo "$filename 未知类型"
fi
done
14、编写脚本判断80端口是否开启,开启则输出ok,未开启则重启httpd服务,并依据重启的状态输出成功or失败(5分钟)
shell
#!/bin/bash
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
15、编写nginx管理脚本实现(15分钟)
nginx_manager.sh start
nginx_manager.sh stop
nginx_manager.sh restart
nginx_manager.sh reload
shell
#!/bin/bash
if [ $# -ne 1 ];then
echo "Usage: $0 start|stop|reload|restart|status"
exit
fi
args=$1
function f1() {
if [ $? -eq 0 ];then
echo "nginx $args 成功"
else
echo "nginx $args 失败"
fi
}
case $1 in
start)
netstat -an |grep ":80\b" &>/dev/null
if [ $? -eq 0 ];then
echo "nginx is already running"
else
/usr/local/nginx/sbin/nginx &>/dev/null
f1
fi
;;
stop)
/usr/local/nginx/sbin/nginx -s stop &>/dev/null
f1
;;
reload)
/usr/local/nginx/sbin/nginx -s reload &>/dev/null
f1
;;
restart)
netstat -an |grep ":80\b" &>/dev/null
# 1、先关闭
if [ $? -eq 0 ];then
/usr/local/nginx/sbin/nginx -s stop
fi
sleep 2
# 2、再启动
/usr/local/nginx/sbin/nginx
# 3、然后判断启动状态
f1
;;
status)
echo "status............"
;;
*)
echo "Usage: $0 start|stop|reload|restart|status"
esac
16、编写猜数字程序,最多允许猜错3次,猜对了显示猜对了,大了告诉猜大了,小了告诉猜小了(10分钟)
shell
#!/bin/bash
num=`echo $((RANDOM%100+1))`
count=0
while :
do
[ $count -eq 3 ] && echo "猜的次数超过3次,退出" && exit
read -p "请输入[1-100]之间的一个数字:" x
[[ ! $x =~ ^[0-9]+$ ]] && echo "必须输入数字" && continue
if [ $x -gt $num ];then
echo "猜大了"
elif [ $x -lt $num ];then
echo "猜小了"
else
echo "猜对了"
break
fi
let count++
done