-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshell-answer
More file actions
347 lines (331 loc) · 6.32 KB
/
Copy pathshell-answer
File metadata and controls
347 lines (331 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
01.
#!/bin/bash
. /etc/init.d/functions
dir=/var/log/disk
[ ! -d $dir ] && mkdir $dir -p
df -h > $dir/$(date +%F).log
02.
#!/bin/bash
. /etc/init.d/functions
awk '{print $1}' 1.log |sort|uniq -c |sort -rn
#awk '{a[$1]++}END{for (ip in a) print a[ip],ip}' 1.log |sort -nr
03.
#!/bin/bash
. /etc/init.d/functions
sum=0
for n in `ps aux|grep -v USER|awk '{print $6}'`
do
sum=$[$sum+$n]
done
echo $sum
04.
#!/bin/bash
. /etc/init.d/functions
ip=10.0.0.20
loss_status=`ping -c 3 -w 3 $ip|grep "packet loss"|awk -F "[,% ]+" '{print $6}'`
[ "`echo $ip |sed 's#[0-9]##g'`" != "..." ] && {
echo "mail $ip 脚本ip地址有错"
exit 2
}
chk(){
if [ ${loss_status} -eq 100 ];then
echo "mail $ip 宕机了"
elif [ ${loss_status} -eq 0 ];then
echo "$ip 在线" &>/dev/null
else
echo "mail $ip 丢包率比较高,丢包率为${loss_status}"
fi
}
main(){
while true
do
chk
sleep 10
done
}
main
05.
#!/bin/bash
. /etc/init.d/functions
cd /abc
ls *.txt &>/dev/null
if [ $? -eq 0 ];then
rename txt txt.bak *.txt
#find /abc -type f -name "*.txt.bak"|xargs -i mv {} {}.bak
##find /abc -type f -name "*.txt"|awk '{print "mv",$1,$1".bak"}'|bash
else
echo "not found *.txt"
exit 2
fi
ls *.bak &>/dev/null
if [ $? -eq 0 ];then
tar czf abc.tar.gz *.bak
rm *.bak -f
else
echo "not found *.bak"
exit 2
fi
ls abc.tar.gz &>/dev/null
if [ $? -eq 0 ];then
tar xf abc.tar.gz
rename txt.bak txt *.txt.bak
else
echo "not foud abc.tar.gz"
exit 2
fi
06.
#!/bin/bash
. /etc/init.d/functions
chk(){
if [ `ss -lntup|grep "\b80\b"|wc -l` -eq 0 ];then
systemctl restart httpd
fi
}
main(){
while true
do
chk
sleep 30
done
}
main
07.
#!/bin/bash
. /etc/init.d/functions
remote_ip=10.0.0.10
bakdir=/backup/mysql
[ `ss -lntup|grep "\b3306\b"|wc -l` -eq 0 ]&&{
echo "mail mysql is down"
exit 2
}
[ ! -d $bakdir ]&& mkdir -p $bakdir
cd $bakdir
mysqldump -uroot -p123456 -B wordpress -x | gzip >wordpress_$(date +%F).sql.gz
rsync -avz wordpress_$(date +%F).sql.gz rsync_backup@${remote_ip}::backup --password-file=/etc/rsync.password
##############
find $bakdir -type f -name "*.sql.gz" -mtime +7 | xargs rm -f
08.
#!/bin/bash
. /etc/init.d/functions
logfile=/data/log/access.log
[ ! -f $logfile ]&&{
echo "not found $logfile"
exit 2
}
chk(){
if [ `grep "/b502/b" $logfile|wc -l`-gt 0 ];then
/etc/init.d/php-fpm restart
fi
}
main(){
while true
do
chk
sleep 10
done
}
main
09.
#!/bin/bash
#author:feko
#version:0.1
#description:...
. /etc/init.d/functions
del_line=`cat -n a.txt |sed -n '1,5p'|sed -n '/[a-zA-Z]/p'|awk '{print $1}'`
sed -r -i '6,10s#[a-zA-Z]##g' a.txt
for i in ${del_line}
do
sed -i "${i}d" a.txt
done
10.
#!/bin/bash
#author:feko
#version:0.1
#description:用shell打印下面这句话中字母数小于6的单词。
. /etc/init.d/functions
world=`echo "Bash also interprets a number of multi-character options."|tr " " "\n"|awk -F "[ .]+" '{print $1}'`
for i in $world
do
if [ `echo "$i" |wc -c` -lt 6 ];then
echo "$i"
fi
done
11.
#!/bin/bash
. /etc/init.d/functions
chk(){
read -e -p "please input 1-4 number:" arg
case $arg in
1)
date
;;
2)
ls
;;
3)
who
;;
4)
pwd
;;
*)
break
;;
esac
}
main(){
while true
do
chk
sleep 3
done
}
main
12.
#!/bin/bash
. /etc/init.d/functions
for i in `seq 0 1 9`
do
id user_0$i
if [ $? -ne 0 ];then
useradd user_0$i
password=`mkpasswd -l 10`
echo "user_0$i $password">>/var/log/mkpasswd.log
echo "$password"|passwd --stdin user_0$i
fi
done
13.
#!/bin/bash
#author:feko
#version1.0
#description:采用了函数,分模块来实现
. /etc/init.d/functions
#变量定义
##pg=`ps -C httpd --no-headers|wc -l`
pg=`pgrep httpd|wc -l`
rehttp="systemctl restart http"
RETVAL=0
RETVAL1=0
RETVAL2=10
#默认10秒检查一次,判断进程数大于500,则返回8,小于则返回4
chk(){
sleep $1
if [ $pg -gt 500 ];then
RETVAL=8
else
RETVAL=4
fi
}
#判断重启是否成功,成功返回8失败返回4
judge_rehttp(){
if [ $RETVAL -eq 8 ];then
$rehttp
[ $? -eq 0 ]&&RETVAL1=8||RETVAL1=4
fi
}
#重启失败则走这条路
restart_failure(){
if [ $RETVAL1 -eq 4 ];then
for i in `seq 6`
do
if [ $RETVAL1 -eq 8 ];then
break
elif [ $RETVAL1 -eq 4 ];then
judge_rehttp
fi
done
[ $i -eq 6 ]&&{
echo "mail admin htptd restart is failure"
exit 2
}
fi
}
#重启成功则走这条路
restart_success(){
if [ $RETVAL1 -eq 8 ];then
RETVAL2=60
chk $RETVAL2
if [ $RETVAL -eq 8 ];then
echo "mail httpd pg is $pg"
exit 3
else
RETVAL2=10
fi
fi
}
#主函数
main(){
while true
do
chk $RETVAL2
judge_rehttp
restart_failure
restart_success
done
}
main
14.
#!/bin/bash
##description:防止恶意攻击
. /etc/init.d/functions
t1=`date +%Y:%H:%M`
logfile=/data/logs/access_log
ban_ip(){
egrep "$t1:[0-9]+" $logfile > /tmp/tmp_last_min.log
awk '{n[$1]++}END{for (i in n) print n[i],i}' /tmp/tmp_last_min.log |sort -nr |awk '$1>100{print $2}'>/tmp/bad_ip.log
n=`cat /tmp/bad_ip.log|wc -l`
if [ $n -ne 0 ];then
for ip in `cat /tmp/bad_ip.log`
do
if [ `iptables -nvL |grep $ip|wc -l` -eq 0 ];then
iptables -I INPUT -s $ip -j REJECT
fi
done
fi
}
pass_ip(){
#pkts #计数器
##iptables -Z 清空这些计数器
iptables -nvL INPUT|sed '1,2d' |awk '$1<5 {print $8}' > /tmp/good_ip.list
n=`cat /tmp/good_ip.log|wc -l`
if [ $n -ne 0 ]
then
for ip in `cat /tmp/good_ip.log`
do
iptables -D INPUT -s $ip -j REJECT
done
fi
iptables -Z
}
t=`date +%M`
if [ "$t" = "00" ] || [ "$t" = "30" ];then
pass_ip
ban_ip
else
ban_ip
fi
15.
#!/bin/bash
. /etc/init.d/functions
a=10
b=150
n=0
echo -n "10 "
for ((a;a<b;n++))
do
tmp=$[2**n]
a=$[a+20+tmp]
echo -n "$a "
done
echo
16.
#!/bin/bash
. /etc/init.d/functions
users_file=/tmp/users_file
awk -F: '{if($3>=1000) print $1,$3}' /etc/passwd>${users_file}
if [ -s ${users_file} ];then
n=`wc -l ${users_file}|awk '{print $1}'`
echo "有${n}普通用户"
else
echo "没有普通用户"
fi