SHELL训练营--day13_shell练习

#输入数字,运行对应命令。

#!/bin/bash

while:
do
    read -p "please input a number: "n
    if [ -z "$n" ]
    then 
        echo "请输入一个纯数字,1~4"
        sleep 3
        continue
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n $n1 ]
    then
        echo "请输入一个纯数字,1~4"
        sleep 3
    else
        break
done

case $n in
    1) date
    ;;
    2) ls
    ;;
    *) echo "请输入一个纯数字,1~4"
    ;;
esac

#批量创建用户
#!/bin/bash
for i in `seq -w 00 09`
do
    useradd user_$i
    p=`mkpasswd -l 10 -s 0`
    echo "user_$i $p" >> /tmp/pass.tmp/pass
    echo "$p"|passwd --stdin user_$i
done

#监控httpd进程
#!/bin/bash
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 [email protected] "apache重启失败" "`head -1 /tmp/httpd.err`"
        exit
}

while:
do
    n=`ps -C httpd --no-heading|wc -l`
    if [ $n -ge 500 ]
    then
        check_service
        sleep 60
        n_new=`ps -C httpd --no-heading|wc -l`
        if [ $n_new -ge 500 ]
        then
            python mail [email protected] "apache重启失败" "请检查服务器"
            exit
        fi
    fi
    sleep 10
done

# 根据日志封IP
#!/bin/bash
block_ip()
{
t1=`date -d "-1 min" +%Y:%H:%M`
log=/data/logs/access_log

egrep "$t1:[0-9+]" $log > /tmp/tmp_last_min.log

awk '{print $1}' /tmp/tmp_last_min.log |sort -n |uniq -c |sort -n |awk `$1>100 {print $2}` >/tmp/bad_ip.list

n=`wc -l /tmp/bad_ip.list|awk '{print $1}'`
if [ $n -ne 0 ]
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
    n=`wc -l /tmp/good_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" -o $t =="30" ]
then
    unblock_ip
    block_ip
else
    block_ip
fi

#算数字
#!/bin/bash
x=10
y=21
for i in `seq 0 15`
do
    echo $x
    x=$[$x+$y]
    z=$[2**$i]
    y=$[$y+$z]
done

猜你喜欢

转载自blog.51cto.com/sincethen/2339161