码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程实战5

时间:2019-08-30 20:47:49      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:技术   cat   sed   tab   rest   批量   head   菜单   apache2   

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

标签:技术   cat   sed   tab   rest   批量   head   菜单   apache2   

原文地址:https://www.cnblogs.com/tanzhirong/p/11436877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!