shell习题100(三)

版权声明:版权所有,转载请注明出处。email:[email protected] https://blog.csdn.net/qq_42334372/article/details/85807047

题目要求

写一个脚本实现如下功能: 
输入一个数字,然后运行对应的一个命令。

显示命令如下:

cmd meau*  1 - date 2 - ls 3 - who 4 - pwd
当输入1时,会运行date, 输入2时运行ls, 以此类推。

核心要点

  • case判断

参考答案

#!/bin/bash
echo "*cmd meau**  1 - date 2 - ls 3 - who 4 - pwd"
read -p "Please input a number: " n
if [ -z "$n" ]
then
    echo "请输入一个纯数字,范围1-4."
    exit
fi

n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
    echo "请输入一个纯数字,范围1-4."
    exit
fi

case $n in
    1)
	date
	;;
    2)
	ls
	;;
    3)
	who
	;;
    4)
	pwd
	;;
    *)
	echo "请输入1-4的数字"
        ;;
esac

题目要求

用shell脚本实现如下需求:

添加user_00 – user_09 10个用户,并且给他们设置一个随机密码,密码要求10位包含大小写字母以及数字,注意需要把每个用户的密码记录到一个日志文件里。
 
提示:

  1. 随机密码使用命令 mkpasswd

  2. 在脚本中给用户设置密码,可以使用echo 然后管道passwd命令

核心要点

  • seq实现数字递增
  • mkpasswd产生随机字符

参考答案

#!/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
    echo $p |passwd --stdin user_$i
done

题目要求

在服务器上,写一个监控脚本,要求如下:

  1. 每隔10s去检测一次服务器上的httpd进程数,如果大于等于500的时候,就需要自动重启一下apache服务,并检测启动是否成功?

  2. 若没有正常启动还需再一次启动,最大不成功数超过5次则需要立即发邮件通知管理员,并且以后不需要再检测!

  3. 如果启动成功后,1分钟后再次检测httpd进程数,若正常则重复之前操作(每隔10s检测一次),若还是大于等于500,那放弃重启并需要发邮件给管理员,然后自动退出该脚本。假设其中发邮件脚本为之前使用的mail.py

核心要点

  • pgrep -l httpd或者ps -C httpd --no-heading检查进程
  • for循环5次计数器

参考答案

#!/bin/bash
check_service()
{
    n=0
    for i in `seq 1 5`
    do
        /usr/local/apache2/bin/apachectl restart 2>/tmp/apache.err
        if [ $? -ne 0 ]
        then
            n=$[$n+1]
        else
            break
        fi
    done
    if [ $n -eq 5 ]
    then
        ##下面的mail.py参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D22Z/mail.py
        python mai.py "[email protected]" "httpd service down" `cat /tmp/apache.err`
        exit
    fi
}   
while true
do
    t_n=`ps -C httpd --no-heading |wc -l`
    if [ $t_n -ge 500 ]
    then
        /usr/local/apache2/bin/apachectl restart
        if [ $? -ne 0 ]
        then
            check_service
        fi
        sleep 60
        t_n=`ps -C httpd --no-heading |wc -l`
        if [ $t_n -ge 500 ]
        then
            python mai.py "[email protected]" "httpd service somth wrong" "the httpd process is busy."
            exit
        fi
    fi
    sleep 10
done

题目要求

需求: 根据web服务器上的访问日志,把一些请求量非常高的ip给拒绝掉!并且每隔半小时把不再发起请求或者请求量很小的ip给解封。
 
假设:

  1. 一分钟内请求量高于100次的IP视为不正常请求。

  2. 访问日志路径为/data/logs/access_log。

用第2例中的1.log作为演示日志

核心要点

  • 统计ip访问次数,排序
  • 如何标记每隔半小时
  • iptables计数器是一个重要的判断指标
  • 函数(封IP、解封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" ] || [ $t == "30" ]
then
   unblock_ip
   block_ip
else
   block_ip
fi

题目要求

请仔细查看如下几个数字的规律,并使用shell脚本输出后面的十个数字。

10 31 53 77  105 141 …….

核心要点

  • 计算两个数值之间的差值

参考答案

#!/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.csdn.net/qq_42334372/article/details/85807047