shell编程实战5

Posted tanzhirong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell编程实战5相关的知识,希望对你有一定的参考价值。

1. 根据菜单中数字入口,执行命令

   技术图片

2. 批量创建用户

   技术图片

3. 监控http的进程

#!/bin/bash

mail_addr=tzr@qq.com
check_service()

c=0
for i in `seq 1 5`
do
/usr/local/apache2/bin/apachectl -k restart 2>/tmp/httpd.err
if [ ! $? -eq 0 ]
then
c=$[$c+1]
else
break
fi
done
if [ $c -eq 5 ]
then
python mail.py $mail_addr "httpd进程数大于500,重启失败!" "head -1 /tmp/httpd.err"
fi

while true
do
n=`ps -C httpd --no-heading|wc -l`
if [ $n -ge 500 ]
then
check_service
sleep 60
new_n=`ps -C httpd --no-heading|wc -l`
if [ $new_n -ge 500 ]
then
python mail.py $mail_addr "httpd服务重启1分钟后进程数量仍大于500" "please check"
exit
fi
fi
sleep 10
done

4. 根据日志内容,对非法IP禁止访问(封ip)

#!/bin/bash

block_ip()

log=/data/logs/access_log
t1=`date -d "-1 min" "+%Y:%H:%M"`
#t2=$1:[0-9][0-9]

egrep "$t1:[0-9]+" $log >/tmp/tmp_lastmin.log
awk ‘print $1‘ /tmp/tmp_lastmin.log|sort -n |uniq -c |sort -n |awk ‘$1>100 print $2‘ >/tmp/bad_ip.list
n=`wc /tmp/bad_ip.list|awk ‘print $1‘`
if [ $n -ne 0 ]
#if [ -s /tmp/bad_ip.list ]
then
for ip in `cat /tmp/bad_ip.list`
do
iptables -I INPUT -s $ip -j REJECT
done
fi

unblock_ip()

iptables -nvl INPUT |sed ‘1d‘|awk ‘$1<5 print $8‘ >/tmp/good_ip.list
for ip in `cat /tmp/goog_ip.list`
n=`wc /tmp/goog_ip.list|awk ‘print $1‘`
if [ $n -ne 0]
then
for ip in `cat /tmp/good_ip.list`
do
iptables -D INPUT -s $ip -j REJECT
done
fi
iptables -z

t=date "+%M"
if [ $t == "00"] || [ $t == "30"]
then
unblock_ip
block_ip
else
block_ip
fi

5. 

以上是关于shell编程实战5的主要内容,如果未能解决你的问题,请参考以下文章

shell编程实战11

shell编程实战13

shell编程企业实战(试题)

Shell脚本编程实战

shell编程

Shell 编程实战