Bash多线程速度问题

时间:2016-02-19 18:38:35

标签: multithreading bash shell openssl brute-force

我正在编写一个解码编码密码的脚本,以便查看原始密码。问题是顺序代码比多线程代码更快。顺序代码持续12秒,多线程持续20秒。为什么?

我使用8 GB Ram + i5-2450M(4CPU~2,4Ghz)+ SSD在基本OS 64位(ubuntu 14.04)上执行。

序列码。

#!/bin/bash

space="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"

if [  $# -le 1 ]
then 
    echo "Ussage: " $0 SALT PASSWORD_CODED
    exit
fi
echo $2
echo $1
for i in $space
do
for j in $space
do
for k in $space
do
    #echo $i$j$k
    variable=$(openssl passwd -crypt -salt "$1" "$i$j$k")
    if [ "$variable" = $2 ]
        then
        echo password found: $i$j$k
        exit
    fi
done
done
done

多线程代码。

space="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"

if [  $# -le 1 ]
then 
    echo "Ussage: " $0 SALT PASSWORD_CODED
    exit
fi

calculate1 () 
{
    for i in $space
    do
    for j in $space
    do
    for k in $space
    do
        variable=$(openssl passwd -crypt -salt "$1" "$k$j$i")
        if [ "$variable" = $2 ]
            then
            echo password found: $k$j$i
            date
            exit
        fi
    done
    done
    done
}

calculate2 () 
{
    for i in $space
    do
    for j in $space
    do
    for k in $space
    do
        variable2=$(openssl passwd -crypt -salt "$1" "$i$j$k")
        if [ "$variable2" = $2 ]
            then
            echo password found: $i$j$k
            date
            exit
        fi
    done
    done
    done
}

date
calculate1 $1 $2 &
calculate2 $1 $2 &

echo waiting
wait

为什么单线程和顺序代码更快?

我可以使用C代码而不是bash吗? 什么与C中的variable2=$(openssl passwd -crypt -salt "$1" "$i$j$k")相似?

1 个答案:

答案 0 :(得分:1)

在26个进程之间划分工作的示例:

#!/bin/bash

s="$1"; p="$2"
printf -v space "%s " {a..z}
echo $s $p

calc() {
  for j in $space; do
    for k in $space; do
      variable=$(openssl passwd -crypt -salt "$s" "$i$j$k")
      if [ "$variable" = "$s$p" ]; then
        echo password found: $i$j$k
        return
      fi
    done
  done
}

for i in $space; do
  calc &
done

wait

测试:./script.sh 6 AGuqwGE0gkDg

输出:

6 AGuqwGE0gkDg
password found: xde