工作中编写的一些小脚本

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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#批量处理域名访问时间
while read line
do
ping -c 3 $line|tail -1  |awk -F "/" '{print $5}'>> result/"$line"_result.txt&
done < wenzhou_domain.txt
sleep 1m
 
while read domain
do
a=`cat result/"$domain"_result.txt`
if [ "$a" = "" ];then
echo unknow >>result.txt
else
echo $a    >>result.txt
fi
done <wenzhou_domain.txt
#批量处理域名状态码
#!/bin/bash
Thread=100
CurFileName="domain.txt"
FifoFile="$.fifo"
mkfifo $FifoFile
exec 6<>$FifoFile
rm $FifoFile
for ((i=0;i<=$Thread;i++));do echo;done >&6
exec 5<$CurFileName
trap 'kill -9 0;exit 1' 1 2 3 15
 
while read -u5 line
do
        read -u6
        {
                STATUS_CODE=$(curl -I -o /dev/null -s -w %{http_code} "$line")
                echo -e "$STATUS_CODE" |tee -a code.txt
                echo >&6
        } &
done
wait
#批量dig取ip
#!/bin/bash
Thread=100
CurFileName="domain.txt"
FifoFile="$.fifo"
mkfifo $FifoFile
exec 6<>$FifoFile
rm $FifoFile
for ((i=0;i<=$Thread;i++));do echo;done >&6
exec 5<$CurFileName
trap 'kill -9 0;exit 1' 1 2 3 15
 
while read -u5 line
do
        read -u6
        {
                STATUS_CODE=$(dig "$line" A +noall +answer |tail -1|awk '{print $5}' )
                echo -e "$line:\t$STATUS_CODE" |tee -a code.txt
                echo >&6
        } &
done
wait
#批量查询端口
1
#!/bin/bash
Thread=100
CurFileName="domain.txt"
FifoFile="$.fifo"
mkfifo $FifoFile
exec 6<>$FifoFile
rm $FifoFile
for ((i=0;i<=$Thread;i++));do echo;done >&6
exec 5<$CurFileName
trap 'kill -9 0;exit 1' 1 2 3 15
 
while read -u5 line
do
        read -u6
        {
                STATUS_CODE=$(nmap -p80,443 "$line" | grep open  )
                echo -"$line\n$STATUS_CODE"|cut -d "/" -f1 |tee -a code.txt
                echo >&6
        } &
done
wait
2
while read domain
do
nmap -sT $domain | grep open >> result/"$domain"_result.txt &
done < domain.txt
sleep 5m
while read Domain
do
        echo =========$Domain============ >>result.txt
        cat result/"$Domain"_result.txt >>result.txt
done < domain.txt
#监控web服务状态
#!/bin/sh
CheckUrl(){
timeout=5
fails=0
success=0
while true
do
    wget --timeout=$timeout --tries=1 http://blog.chinaunix.net/uid-23929712-id-2650421.html -q -O /dev/null
    if [ $? -ne 0 ]
        then
        let fails=fails+1
        else
        let success=success+1
    fi 
    if [ $success -ge 1 ]
        then
        echo success
        Critical="sys is up."
        echo $Critical|tee|mail -s "$Critical" hejianlai@dnion.com
        exit 0
    fi
    if [ $fails -ge 2 ]
        then
        Critical="sys is down."
        echo $Critical|tee|mail -s "$Critical" hejianlai@dnion.com
        exit 2
    fi
    done
     
}
CheckUrl
#判断系统内存大小
#/bin/sh
FreeMem=`free -m|awk 'NR==3 {print $NF}'`
CHARS="Current memory is $FreeMem"
if [ $FreeMem -lt 100 ]
then
    echo $CHARS|tee /tmp/messages.txt
        mail -s "`date +%F-%T`$CHARS" hejianlai@dnion </tmp/messages.txt
fi
#DDos攻击
#!/bin/sh
file=$1
while true
do
        awk '{print $1}' $1|grep -v "^$"|sort|uniq -c >/tmp/tmp.log
        exec </tmp/tmp.log
        while read line
        do
        ip=`echo $line|awk '{print $2}'`
        count=`echo $line|awk '{print $1}'`
        if [ $count -gt 500 ] && [ `iptables -L -n|grep "$ip"|wc -l` -lt 1 ]
        then
        iptables -I INPUT -s $ip -j DROP
        echo "$line is dropped" >>/tmp/droplist_$(date +%F).log
        fi
        done
sleep 1m
done
#去重输出到原文件
#! /bin/bash
while read gsa
do
{
while read yijiazai
do
if [ $gsa = $yijiazai ]; then
echo $gsa >>chongfu_domain.txt
sed -i "/$gsa/d" vaas.txt
#sed -i "/$gsa/d" yijiazai_domain.txt
fi
done < bukejia_domain.txt
}
done < vaas.txt
#去重输出到新文件
#! /bin/bash
while read https_d
do
{
while read diyipi_d
do
if [ $https_d = $diyipi_d ]; then
echo $https_d >>quchong_and_newfile.txt
sed -i "/$https_d/d" diyipi_domain.txt
fi
done < diyipi_domain.txt
}
done < https.txt
#过滤相同
#! /bin/bash
while read zuihouyipi
do
{
while read wangsu666_d
do
if [ $wangsu666_d = $zuihouyipi ]; then
echo $wangsu666_d >> qieheidomain.txt
fi
done < wangsu666domain.txt
}
done < 161domain.txt
#批量取出对应域名url
#!/bin/sh
while read line
do
        cat access.log*|awk '{print $5,$8}'|grep 200|awk '{print $2}'|grep $line|sort -R|head -2 >>rulse.txt
 
done < domain.txt
判断mysql服务是否开启的方法:
#!/bin/sh
echo method1-------------------
if [ `netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'` -eq 3306 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
echo method2-------------------
if [ "`netstat -lnt|grep 3306|awk -F "[ :]+" '{print $5}'`" = "3306" ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
  
echo method3-------------------
if [ `netstat -lntup|grep mysqld|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
echo method4-------------------
if [ `lsof -i tcp:3306|wc -l` -gt 0 ]
then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
echo method5-------------------
[ `rpm -qa nmap|wc -l` -lt 1 ] && yum install nmap -y &>/dev/null
if [ `nmap 127.0.0.1 -p 3306 2>/dev/null|grep open|wc -l` -gt 0 ]
  then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
echo method6-------------------
[ `rpm -qa nc|wc -l` -lt 1 ] && yum install nc -y &>/dev/null
if [ `nc -w 2  127.0.0.1 3306 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ]
  then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
echo method7-------------------
if [ `ps -ef|grep -v grep|grep mysql|wc -l` -ge 1 ]
  then
    echo "MySQL is Running."
else
    echo "MySQL is Stopped."
    /etc/init.d/mysqld start
fi
判断http服务是否开启的方法:
#!/bin/sh
echo http method1-------------------
if [ `netstat -lnt|grep 80|awk -F "[ :]+" '{print $5}'` -eq 80 ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
echo http method2-------------------
if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $5}'`" = "80" ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
  
  
  
echo http method3-------------------
if [ `netstat -lntup|grep nginx|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
echo http method4-------------------
if [ `lsof -i tcp:80|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
echo http method5-------------------
[ `rpm -qa nmap|wc -l` -lt 1 ] && yum install nmap -y &>/dev/null
if [ `nmap 127.0.0.1 -p 80 2>/dev/null|grep open|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
echo http method6-------------------
[ `rpm -qa nc|wc -l` -lt 1 ] && yum install nc -y &>/dev/null
if [ `nc -w 2  127.0.0.1 80 &>/dev/null&&echo ok|grep ok|wc -l` -gt 0 ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
echo http method7-------------------
if [ `ps -ef|grep -v grep|grep nginx|wc -l` -ge 1 ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
  
echo http method8-------------------
if [[ `curl -I -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1` =~ [23]0[012] ]]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
  
  
echo http method9-------------------
if [ `curl -I http://127.0.0.1 2>/dev/null|head -1|egrep "200|302|301"|wc -l` -eq 1  ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
echo http method10-------------------
if [ "`curl -s http://127.0.0.1`" = "oldboy"  ]
  then
    echo "Nginx is Running."
else
    echo "Nginx is Stopped."
    /etc/init.d/nginx start
fi
  
ssh服务开启,关闭脚本
#!/bin/sh
path=/etc/init.d/sshd
if [ $# -ne 1 ]
        then
        echo $"usage:$0{start|stop|resatrt}"
        exit 1
fi
if [ "$1" = "start" ]
        then
        $path start
        if [ `netstat -lntup|grep sshd|wc -l` -ge 1 ]
        then
        echo "sshd is started"
        exit 0
        fi
elif [ "$1" = "stop" ]
        then
        $path  stop
        if [ `netstat -lntup|grep sshd|wc -l` -eq 0 ]
        then
        echo "sshd is stop"
        exit 0
        fi
elif [ "$1" = "restart" ]
        then
        $path  stop
        sleep 2
        $path  start
         if [ `netstat -lntup|grep sshd|wc -l` -ge 1 ]
        then
        echo "sshd is restarted"
        else
        echo echo $"usage:$0{start|stop|resatrt}"
        exit 1
         fi
fi
安装lamp,lnmp脚本
#!/bin/sh
path=/root/scripts
[ ! -d "$path" ]&& mkdir $path
cat <<EOF
        1.[install lamp]
        2.[install lnmp]
        3.[exit]
        pls input the num you want:
EOF
read num
[ $num -eq 1 ]&&{
        echo "start installing lamp."
        sleep 2
        [ -x "$path/lamp.sh" ]||{
        echo "$path/lamp.sh does not exist or can be exec"
        exit 1
}
        $path/lamp.sh
        exit $?
}
[ $num -eq 2 ]&&{
        echo "start installing lnmp."
        sleep 2
        [ -x "$path/lnmp.sh" ]||{
        echo "$path/lnmp.sh does not exist or can be exec"
        exit 1
}            
        $path/lnmp.sh
        exit $?
}
[ $num -eq 3 ]&&{
        echo bye
        exit 3
}
[ ! $num -eq 1 -o ! $num -eq 2 -o ! $num -eq 3 ]&&{
        echo "the num you input must be{1|2|3}"
        echo "Input ERROR"
        exit 4
}
 
跳板机自动输入密码查看
#!/bin/expect
spawn ssh root@192.168.160.133 uptime
expect "*password"
send "123\n"
expect eof
 
批量生成随机字符文件名
#!/bin/sh
Path=/oldboy
[ -d "$Path" ]||mkir -p $Path
for n in `seq 10`
do
        random=`openssl rand -base64 40|sed 's#[^a-z]##g'|cut -c 2-8`
        touch $Path/${random}_oldboy.html
done
 
防止恶意破解root账户的脚本
#!/bin/sh
cat /var/log/secure | awk '/Failed/{print $(NF-3)}' |sort|uniq -c|awk
'{print $2"="$1}' >>/root/black.txt
DENY=50
for i in $(cat /root/black.txt)
do
        IP=$($i| awk -F'=' '{print $1}')
        NUM=$($i| awk -F'=' '{print $2}')
        if [ $NUM -gt $DENY ];then
        grep $IP /etc/hosts.deny > /dev/null
        if [ $? -gt 0 ];then
        echo "ssh:$IP" >> /etc/hosts.deny
        fi
        fi
done
#执行shell脚本获取oracle数据库信息
#!/bin/bash
source ~/.bash_profile
get_info (){
sqlplus user/password <<EOF
--注册总人数
select count(*) from api_real_name_authentication where ARNA_SUBMITTIME >= '20181026';
--过闸总通行次数
select count(*) from api_entry_flow where AEF_ENTRYTIME >= '20181026' and AEF_LINENO<>'1';
--万胜围
select count(*) from api_entry_flow where AEF_ENTRYTIME >= '20181026' and AEF_LINENO='4';
--珠江新城
select count (*) from api_entry_flow where AEF_ENTRYTIME >= '20181026' and AEF_LINENO='3';
--嘉禾望岗
select count(*) from api_entry_flow where AEF_ENTRYTIME >= '20181026' and AEF_LINENO='2';
EOF
}
get_info|grep COUNT -A 3 >result.txt
a=`cat result.txt|sed -n '3p'|sed s/[[:space:]]//g`
b=`cat result.txt|sed -n '8p'|sed s/[[:space:]]//g`
c=`cat result.txt|sed -n '13p'|sed s/[[:space:]]//g`
d=`cat result.txt|sed -n '18p'|sed s/[[:space:]]//g`
e=`cat result.txt|sed -n '23p'|sed s/[[:space:]]//g`
echo "
智慧安检总注册用户数:${a}
过闸总通行次数:${b}
万胜围:${c}
珠江新城:${d}
嘉禾望岗:${e}
" >zhihuianjian.txt

 

posted @   西门运维  阅读(625)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示