面試題18:破解RANDOM隨機數案例程序員
已知下面的字符串是經過RANDOM隨機數變量md5sum後,再截取一部分連續字符串的結果,請破解這些字符串對應的使用md5sum處理前的RANDOM對應的數字?面試
21029299shell
00205d1csegmentfault
a3da1677bash
1f6d12dd網站
890684bthis
[root@jenkins scripts]# cat aa.txt
21029299
00205d1c
a3da1677
1f6d12dd
890684bspa
[root@jenkins scripts]# cat rd_mat.sh #!/bin/bash ############################################################## # File Name: rd_mat.sh # Version: V1.0 # Author: gaobo # Organization: 641627690@qq.com # Created Time : 2017-12-07 19:57:59 # Description: ############################################################## for ((i=0;i<=32767;i++)) do for j in `cat /server/scripts/aa.txt` do #echo "$(echo $i|md5sum|cut -c 1-8) ${j}" if [ "$(echo $i|md5sum|cut -c 1-8)" == "${j}" ] then echo $i fi done done
[root@jenkins scripts]# sh rd_mat.sh
1346
7041
25345
256673d
方法二:代理
[root@jenkins scripts]# cat rd_mat.sh #!/bin/bash ############################################################## # File Name: rd_mat.sh # Version: V1.0 # Author: gaobo # Organization: 641627690@qq.com # Created Time : 2017-12-07 19:57:59 # Description: ############################################################## for ((i=0;i<=32767;i++)) do for j in `cat /server/scripts/aa.txt` do if [[ "$(echo $i|md5sum)" =~ "${j}" ]] then echo $i fi done done
如今每次分析網站日誌的時候都須要判斷百度蜘蛛是否是真實的蜘蛛,nslookup以後須要判斷結果中是否包含「baidu」字符串 如下給出一些shell中判斷字符串包含的方法,來源程序員問答網站 stackoverflow 以及segmentfault。 方法一:利用grep查找 strA="long string" strB="string" result=$(echo $strA | grep "${strB}") if [[ "$result" != "" ]] then echo "包含" else echo "不包含" fi 先打印長字符串,而後在長字符串中 grep 查找要搜索的字符串,用變量result記錄結果 若是結果不爲空,說明strA包含strB。若是結果爲空,說明不包含。 這個方法充分利用了grep 的特性,最爲簡潔。 方法二:利用字符串運算符 strA="helloworld" strB="low" if [[ $strA =~ $strB ]] then echo "包含" else echo "不包含" fi 利用字符串運算符 =~ 直接判斷strA是否包含strB。(這不是比第一個方法還要簡潔嗎!) 方法三:利用通配符 A="helloworld" B="low" if [[ $A == *$B* ]] then echo "包含" else echo "不包含" fi 這個也很easy,用通配符*號代理strA中非strB的部分,若是結果相等說明包含,反之不包含。 方法四:利用case in 語句 thisString="1 2 3 4 5" # 源字符串 searchString="1 2" # 搜索字符串 case $thisString in *"$searchString"*) echo Enemy Spot ;; *) echo nope ;; esa 這個就比較複雜了,case in 我尚未接觸到,不過既然有比較簡單的方法何須如此 方法五:利用替換 STRING_A=$1 STRING_B=$2 if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]] then ## is not substring. echo N return 0 else ## is substring. echo Y return 1 fi 這個也挺複雜