用shell写的字典生成工具
#!/bin/bash
#by leo108
#使用方式:./genpwd.sh 密码类型 密码长度 生成文件
#密码类型如果是1,则是数字+字母,如果是其他则是数字
#例如:./genpwd.sh 1 4 pwd.txt,生成长度为4的数字+字母密码
type=$1
length=$2
output=$3
if [ $type -eq "1" ]; then
g_Dict=( 0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z )
else
g_Dict=( 0 1 2 3 4 5 6 7 8 9 )
fi
count=0
total=1
if [ -e tmp.txt ]; then
rm tmp.txt
fi
for ((i=0;i<$length;i++));
do
let "total*=${#g_Dict[@]}"
done
echo "Total:"$total
declare -a Dict
declare -a Array
bStop=1
for ((i=0;i<$length;i++));
do
Array[$i]=0
done
while [ $bStop != 0 ]; do
for ((i=0;i<$length;i++));
do
Dict[$i]=${g_Dict[${Array[$i]}]}
done
echo ${Dict[@]} >> tmp.txt
let "count+=1"
let "tmp=count%10000"
if [ $tmp -eq 0 ]; then
echo $count" Generated"
fi
for ((j=$length-1;j>=0;j--));
do
let "Array[$j]+=1"
if [ ${#g_Dict[@]} -ne ${Array[$j]} ]; then
break
else
Array[$j]=0
if [ $j -eq 0 ]; then
bStop=0
fi
fi
done
done
sed -r 's/ +//g' tmp.txt >> $output
echo "Finish"
rm tmp.txt
参考来源:http://www.freebuf.com/tools/4086.html
# ./genpwd.sh 1 4 pwd.txt
Total:1679616
10000 Generated
20000 Generated
……
总觉得效率还是太低了。。。{这时就应该是多线程发挥威力的地方了!}
下面这段shell代码是之前发现的,今天碰到上面这个(因为觉得上面这个效率低而且自定义性不够强)所以突然想起来了,用Everything搜索出来,然后执行看了下效果,觉得还不错,就先记下来了,放在下面:
#!/bin/bash
## Koen Noens, December 2007
## word generator : random 'words'
## with given length, or range
## with choice of wharacters to be used
chars0="a b c d e f g h i j k l m n o p q r s t u v w x y z"
chars1="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
chars2="0 1 2 3 4 5 6 7 8 9"
chars3="& | é @ € " # ' ( § ^ è ! ç { à } ) - _ ° ^ " $ [ ] % ù £ µ = + ~ : / ; . , ? "
function increment { #recursive or iterative ???
INPUT=$1
[[ ${#INPUT} -ge $minlen ]] && echo $1
if [[ ${#INPUT} -lt $maxlen ]]; then
for c in $chars; do
increment $1$c
done
fi
}
function showUsage {
echo "$0 MAXLEN [MINLEN] [a [A [n [p]]]]"
echo
echo " MAXLEN integer Maximum lenght, or specific lengt if MINLEN is omitted"
echo " MINLEN integer Minimum lenght, equals MAXLEN if omitted"
echo ""
echo " characters:"
echo " a lower case a-z"
echo " A Uppercase A-Z"
echo " n numeric 0-9"
echo " p punctuation . ?"
}
## argument handler
[[ "$1" = "" ]] && showUsage && exit
maxlen=$1
[[ $2 -eq 0 ]] && minlen=$maxlen || minlen=$2
for arg in "$@"; do # "$@" ???
case $arg in
a) chars="$chars $chars0" ;;
A) chars="$chars $chars1" ;;
n) chars="$chars $chars2" ;;
p) chars="$chars $chars3" ;;
esac;
done
#fallback
[[ "$chars" = "" ]] && chars="$chars0" # Default is $chars0
## kickof
for i in $chars; do # call the function defined before
increment $i
done
来源:http://users.telenet.be/mydotcom/program/shell/generatewords.txt
注:在该网站下还有不少别的很好的shell脚本。
使用示例(保存为generator.sh,添加可执行权限):
$ ./generator.sh 4 4 a #生成4位形如:aaaa-zzzz的字符组合
$ ./generator.sh 4 4 A #生成4位形如:AAAA-ZZZZ的字符组合
$ ./generator.sh 4 4 n #生成4位形如:0000-1111的数字组合