- 论坛徽章:
- 0
|
本帖最后由 helloiac 于 2014-05-07 11:12 编辑
2013 shell 编程大赛第一题
有一小撮别有用心的人,他们会想办法获取论坛的用户名列表,然后用弱密码逐个尝试每一个用户名。针对这一情况,论坛程序会把每一次登陆失败的记录放在日志里面。请编写脚本找出尝试登陆失败次数最多的 top 10 的IP地址和登陆失败次数。 <?PHP exit;?> 1357510682 oahgpbzzl rz6***7W 91.224.246.183
<?PHP exit;?> 1357511540 yvbykozi ZRS***38 188.165.205.101
<?PHP exit;?> 1357511656 AlexanderZS xV***R3 94.180.210.229
<?PHP exit;?> 1357512059 deefadvirmdak E9q***kV 111.73.45.49
<?PHP exit;?> 1357513658 michaelkorstian Vq2***jK 91.224.247.24
<?PHP exit;?> 1357513659 michaelkorsbentian Vq2***jK 91.224.247.24
<?PHP exit;?> 1357513953 woodhzyl fK***bc 175.42.46.63
<?PHP exit;?> 1357516630 Cruirmtig c9q***rZ 128.72.115.220
<?PHP exit;?> 1357516641 payprophare 8Ct***tJ 220.250.58.171
<?PHP exit;?> 1357517851 liszdnjn ZRS***38 142.4.117.83
<?PHP exit;?> 1357518471 asdfgt26z fK***bc 58.22.19.16
<?PHP exit;?> 1357518701 lKiSrHkBeR 0w3***uq 27.159.207.4
<?PHP exit;?> 1357518746 zAoPoTeXcL 0w3***uq 27.159.207.4
<?PHP exit;?> 1357518927 asdfgt25w fK***bc 36.251.6.156
<?PHP exit;?> 1357519355 chuntyaxonync tgg***9X 117.26.200.33
<?PHP exit;?> 1357519365 xiyjizqv ZRS***38 192.74.224.245
<?PHP exit;?> 1357521148 asdfgt23a fK***bc 175.42.46.164
<?PHP exit;?> 1357521210 jeffycf df22b31d***8d9a3 15.211.201.81
<?PHP exit;?> 1357521220 jeffycf df22b31d***8d9a3 15.211.201.81 - [cbb@ cbb-HP shell-contest]$ ls -sh
- total 1.6G
- 4.0K 1-1.sh 4.0K 1.sh 4.0K 2.sh 4.0K file.list 1.6G illegal.log 4.0K time.sh 8.0K wls_ms1.out
- [cbb@ cbb-HP shell-contest]$ cat 1.sh
- #!/bin/bash
- time0=`date +%s%N|cut -c 10-13`
- awk '{a[$6]++}END{for(i in a)print a[i],i|"sort -n|tail -10"}' illegal.log
- time1=`date +%s%N|cut -c 10-13`
- s=$(($time1-$time0))
- echo "time is ${s:0:1}.${s:1} sec"
- [cbb@ cbb-HP shell-contest]$ cat 1-1.sh
- #!/bin/bash
- time0=`date +%s%N|cut -c 10-13`
- awk '{a[$6]++}END{for(i in a)print a[i],i}' illegal.log |sort -n |tail -10
- time1=`date +%s%N|cut -c 10-13`
- s=$(($time1-$time0))
- echo "time is ${s:0:1}.${s:1} sec"
- [cbb@ cbb-HP shell-contest]$ bash 1.sh
- 535572 74.91.23.27
- 620136 222.186.26.26
- 648324 111.73.45.13
- 761076 142.4.117.83
- 789264 220.250.58.172
- 817452 220.250.52.90
- 845640
- 845640 220.250.58.171
- 1014768 220.250.58.170
- 1973160 220.250.52.66
- time is 4.76 sec
- [cbb@ cbb-HP shell-contest]$ bash 1-1.sh
- 535572 74.91.23.27
- 620136 222.186.26.26
- 648324 111.73.45.13
- 761076 142.4.117.83
- 789264 220.250.58.172
- 817452 220.250.52.90
- 845640
- 845640 220.250.58.171
- 1014768 220.250.58.170
- 1973160 220.250.52.66
- time is 3.72 sec
复制代码 我看到两种不同的写法- awk '{a[$6]++}END{for(i in a)print a[i],i|"sort -n|tail -10"}' illegal.log
- awk '{a[$NF]++}END{for(i in a)print i,a[i]}' illegal.log | sort -nrk +2 |head -10
复制代码 问题:
为啥第一种写法比第二种写法执行速度慢 |
|