Share seven shell script examples --shell script practice necessary

Outline

See more examples of shell scripts will naturally have ideas to write shell scripts, so I recommend looking generally more examples to practice the script shell script. Sharing a few shell script examples below.


1, monitoring the situation Nginx access log 502, and accordingly the operation

Assuming the server environment lnmp, recently visited phenomenon often 502 and 502 errors after restart php-fpm services disappear, and therefore need to write scripts to monitor, once the 502, then automatically restart php-fpm services.

Scene #: 
# 1 access log file path:. /Data/log/access.log
. Endless loop # 2 script, once every 10 seconds is detected, the log number of 10 seconds to 300, the ratio of occurrence of not less than 502 10% (30) will need to restart php-fpm service
# 3 reboot command:. /etc/init.d/php-fpm restart
# / bin / bash!
############# ##############################################
# monitoring Nginx access log 502, the corresponding action and do
########################################## #################
log = / the Data / log / access.log
N = 30 # set threshold
the while:
do
# view the latest 300 access logs and statistics 502 times
ERR = 300 $ `tail -n log | grep -C '502"' `
IF [ERR -ge $ $ N]
the then
/etc/init.d/php-fpm the restart 2> / dev / null
# setting 60s delayed prevent script bug cause an infinite restart php-fpm service
SLEEP 60
fi
SLEEP 10
DONE
Share seven shell script examples --shell script practice necessary

 


2, the front line of the five elements in a document that contains letters delete, delete all the letters 6-10 line contains

1) Prepare the test file named 2.txt

Line 11234567 does not contain the letter 
row 2 56789BBBBBB
line 3 67890CCCCCCCC
line 4 78asdfDDDDDDDDD
line 5 123456EEEEEEEE
line 6 1234567ASDF
line 7 56789ASDF
line 8 67890ASDF
line 9 78asdfADSF
line 10 123456AAAA
line 11 67890ASDF
line 12 78asdfADSF
line 13 123456AAAA

2) script as follows:

#! / bin / bash 
############################################ ##################
# line before the five elements in a document that contains letters delete, delete all the letters 6-10 line contains
######## ################################################## ####
Sed -n '1,5'p 2.txt | Sed' / [the Z-zA-A] / 'D
Sed -n' 6,10'p 2.txt | S Sed '/ [A- the Z--zA] // 'G
sed -n '11, $' the p-2.txt
# final results just print the results on the screen, if you want to change the file directly, the output can be written to a temporary file, and then replace 2. txt or use the -i option
Share seven shell script examples --shell script practice necessary

 


3, with the print shell is less than the number of letters in the example sentence word 6

#示例语句:
#Bash also interprets a number of multi-character options.
#!/bin/bash
##############################################################
#shell打印示例语句中字母数小于6的单词
##############################################################
for s in Bash also interprets a number of multi-character options.
do
n=`echo $s|wc -c`
if [ $n -lt 6 ]
then
echo $s
fi
done
Share seven shell script examples --shell script practice necessary

 


4、输入数字运行相应命令

#!/bin/bash
##############################################################
#输入数字运行相应命令
##############################################################
echo "*cmd menu* 1-date 2-ls 3-who 4-pwd 0-exit "
while :
do
#捕获用户键入值
read -p "please input number :" n
n1=`echo $n|sed s'/[0-9]//'g`
#空输入检测
if [ -z "$n" ]
then
continue
fi
#非数字输入检测
if [ -n "$n1" ]
then
exit 0
fi
break
done
case $n in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
0)
break
;;
#输入数字非1-4的提示
*)
echo "please input number is [1-4]"
esac
Share seven shell script examples --shell script practice necessary

 

Share seven shell script examples --shell script practice necessary

 


5、创建10个用户,并分别设置密码,密码要求10位且包含大小写字母以及数字,最后需要把每个用户的密码存在指定文件中

#!/bin/bash
##############################################################
#创建10个用户,并分别设置密码,密码要求10位且包含大小写字母以及数字
#最后需要把每个用户的密码存在指定文件中
#前提条件:安装mkpasswd命令
##############################################################
#生成10个用户的序列(00-09)
for u in `seq -w 0 09`
do
#创建用户
useradd user_$u
#生成密码
p=`mkpasswd -s 0 -l 10`
#从标准输入中读取密码进行修改(不安全)
echo $p|passwd --stdin user_$u
#常规修改密码
echo -e "$p\n$p"|passwd user_$u
#将创建的用户及对应的密码记录到日志文件中
echo "user_$u $p" >> /tmp/userpassword
done
Share seven shell script examples --shell script practice necessary

 

Share seven shell script examples --shell script practice necessary

 


6、监控httpd的进程数,根据监控情况做相应处理

#!/bin/bash
###############################################################################################################################
#需求:
#1.每隔10s监控httpd的进程数,若进程数大于等于500,则自动重启Apache服务,并检测服务是否重启成功
#2.若未成功则需要再次启动,若重启5次依旧没有成功,则向管理员发送告警邮件,并退出检测
#3.如果启动成功,则等待1分钟后再次检测httpd进程数,若进程数正常,则恢复正常检测(10s一次),否则放弃重启并向管理员发送告警邮件,并退出检测
###############################################################################################################################
#计数器函数
check_service()
{
j=0
for i in `seq 1 5`
do
#重启Apache的命令
/usr/local/apache2/bin/apachectl restart 2> /var/log/httpderr.log
#判断服务是否重启成功
if [ $? -eq 0 ]
then
break
else
j=$[$j+1]
fi
#判断服务是否已尝试重启5次
if [ $j -eq 5 ]
then
mail.py
exit
fi
done
}
while :
do
n=`pgrep -l httpd|wc -l`
#判断httpd服务进程数是否超过500
if [ $n -gt 500 ]
then
/usr/local/apache2/bin/apachectl restart
if [ $? -ne 0 ]
then
check_service
else
sleep 60
n2=`pgrep -l httpd|wc -l`
#判断重启后是否依旧超过500
if [ $n2 -gt 500 ]
then
mail.py
exit
fi
fi
fi
#每隔10s检测一次
sleep 10
done
Share seven shell script examples --shell script practice necessary

 

Share seven shell script examples --shell script practice necessary

 


7、根据web访问日志,封禁请求量异常的IP,如IP在半小时后恢复正常,则解除封禁

#!/bin/bash
####################################################################################
#根据web访问日志,封禁请求量异常的IP,如IP在半小时后恢复正常,则解除封禁
####################################################################################
logfile=/data/log/access.log
#显示一分钟前的小时和分钟
d1=`date -d "-1 minute" +%H%M`
d2=`date +%M`
ipt=/sbin/iptables
ips=/tmp/ips.txt
block()
{
#将一分钟前的日志全部过滤出来并提取IP以及统计访问次数
grep '$d1:' $logfile|awk '{print $1}'|sort -n|uniq -c|sort -n > $ips
#利用for循环将次数超过100的IP依次遍历出来并予以封禁
for i in `awk '$1>100 {print $2}' $ips`
do
$ipt -I INPUT -p tcp --dport 80 -s $i -j REJECT
echo "`date +%F-%T` $i" >> /tmp/badip.log
DONE
}
unblock ()
{
# pkts after the number of generated IP ban of less than 10, traversing be deblocked
for a in `$ ipt -nvL INPUT --line-numbers | grep '0.0.0.0/0'|awk' 2 $ <{10}. 1 Print $ '| Sort -nr`
do
$ A $ IPT -D the INPUT
DONE
$ IPT the -Z
}
# decapsulation function performed when the time 00 and 30 min time-
if [$ d2 -eq "00" ] || [$ D2 -eq "30"]
the then
# resealing solution first, just because the number of banned pkts generated little
unblock
Block
the else
Block
Fi
Share seven shell script examples --shell script practice necessary

Guess you like

Origin www.cnblogs.com/cangqinglang/p/11330596.html