Write a script to achieve the following functions:
Enter a number, and then run a corresponding command.
Display command is as follows:
* cmd meau ** 1 - date 2 - ls 3 - who 4 - pwd
When input 1 DATE run, run-time input 2 ls, and so on.
The core elements
- case judgment
Questions asked
A Ming teacher Answers
Answers
#!/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
Questions asked
Shell script with the following requirements:
Add user_00 - user_09 10 users and give them to set a random password, password requirements 10 contains uppercase and lowercase letters and numbers, pay attention to the needs of each user's password records to a log file.
prompt:
-
Random password using the command mkpasswd
- User password in the script, then you can use the echo command passwd pipeline
The core elements
- seq digital increments
- mkpasswd generate random characters
seq -w 00 09 achieve two consecutive numbers
mkpasswd -l 10 // 10 characters
mkpasswd -l 10 -s 0 // 0 and no special symbols special symbols of
echo -e "passwd \ npasswd \ n " | passed user1 method for a user to set a password for the user
echo "sdfewdsfewfew12d" | passwd --stdin user Method Two user to set a password for the user
A Ming teacher Answers
Answers
#!/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
Questions asked
On the server, write a script monitoring requirements are as follows:
-
Every 10s to detect the number of httpd processes on a server, if 500 or more of the time, you need to automatically restart the apache service, and to detect the success of start?
-
If you do not start normally need to start again, more than five times the maximum number of unsuccessful need to immediately notify the administrator e-mail, and later do not need to detect!
- If successful start, one minute after the test again a few httpd process, if repeated before the normal operation (tested once every 10s), if greater than or equal to 500, reboot and give up that need to send e-mail to the administrator, and then automatically exit the script . Assume mail.py mail script which is used before
The core elements
- pgrep -l httpd or ps -C httpd --no-heading the inspection process
- for 5 cycles counter
A Ming teacher Answers
Answers
#!/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 …….
#### 核心要点
* 计算两个数值之间的差值
21 22 24 28 36
#### 参考答案
#!/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