【发布时间】:2014-06-13 06:04:02
【问题描述】:
这是一个无限循环的问题,cmets 的小伙伴们给我指正,如果你愿意,可以把它拿下来。
我在这里阅读了其他一些问题和答案,并尝试实施这些建议,但无济于事。因为我不是在编写专有代码,所以我可以完整地发布它,它不会那么长,但我希望堆栈交换不介意......另外,随意使用和/或修改你想要的。
#!/usr/bin/env python2.7
import sys, random, subprocess, signal
def main(argv=None):
if argv is None:
argv = sys.argv
def signal_handler(signal, frame):
fdHT.close()
fdWL.close()
print '\n'
return 1
signal.signal(signal.SIGINT, signal_handler)
pathToWL = str(sys.argv[1])
pathForHT = str(sys.argv[2])
mId = str(sys.argv[3])
SaltCharSet = str("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 0 1 2 3 4 5 6 7 8 9")
SaltCharSet = SaltCharSet.split(" ")
try:
fdWL = open(pathToWL, 'r')
except:
print "Could not open wordlist file."
return 2
try:
fdHT = open(pathForHT, 'a')
except:
print "Could not open file for hash table"
fdWL.close()
return 3
#We have our wordlist now step through the thing and start generating hashes.
toStop = False
#cursor = 0 #Use the cursor later once this program evolves
#print 'Entering while 1'
while(toStop == False):
try:
ln = str(fdWL.readline())
except:
fdHT.close()
fdWL.close()
return 4
if ln == '':
toStop = True #This should have been an ASSIGNMENT not a test, was ==
ln = ln.strip("\n")
ln = ln.strip("\r")
if len(ln) < 6:
continue
# create random salts
# send ln, id, and salts to hashpipe
salt = []
lenOfSalt = random.randint(6,16)
#print 'Entering while 2'
while(len(salt) < lenOfSalt + 1):
aORn = random.randint(0,1)
if aORn == 0:# Its a letter
uORl = random.randint(0,1)
if uORl == 0:
salt.append(SaltCharSet[(random.randint(0,25))].upper())
elif uORl == 1:
salt.append(SaltCharSet[(random.randint(0,25))].lower())
else:
print "Random Int 'uORl' out of bounds"
fdHT.close()
fdWL.close()
toStop = True
return 5 # I don't know what happened
elif aORn == 1:# Its a number
salt.append(SaltCharSet[(random.randint(26, 35))])
else:
print "Random Int 'aORn' out of bounds"
fdHT.close()
fdWL.close()
toStop = True
return 6 # I don't know what happened
#Generated Salt
salt = "".join(salt)
wholeArg2 = str("$"+mId+"$"+salt)
try:
mHash = str(subprocess.check_output([r"hashpipe", ln, wholeArg2]))
except:
print " error getting hash"
#Clean-up
fdHT.close()
fdWL.close()
toStop = True
return 7
#Generated hash, now write it to the fdHT file
print str(ln+"\t"+mHash)
fdHT.write(str(ln+"\t"+mHash))
#cursor = fdWL.tell()
print 'done with whiles'
fdHT.close()
fdWL.close()
return 0
if __name__ == "__main__":
sys.exit(main())
这个脚本调用了我编写的一个小 C 程序,使用 GNUs crypt_r() 函数对字符串进行哈希处理...它挂在最后,我必须使用 ctrl-c 来保释...这里是一些示例输出。 ..
zurumba`tico $6$96u6sUy05rM69$1NLxLYXS9tAf05szgV0/GH6pvykOPsuEIlGxOkDOMNEixGiN8oeTG.xxIq/D19YpArMWtD1xJMG9sKWgA9xzK/
zurupeto $6$O2510Y900o02008$BO2OadT8Bvje78C2JhuZ6r/.iJHz.s9UfET8MU93iGy57bbe/qh9/Uj4jSkVSCyknegnkAB2JF7vRgWohkGVI0
zutana $6$Ur2i9m95$E2WqrEnld4aPa1bYAlCNnMEE0nmwxNlfB9ozVc3I6NCKXHqnSyspZrqIq2usqNf2JwlVF1myhqIn26a71Dm510
zutano $6$8x482Lar4qj$LupCZ9t2ImG.nRVH9xHPsyyx9emiImNTRaErxNrtsGyWjeO3XZLzj.F1D3eQOsiurQeQMWeQ3lF5ef.o3iesg.
zuzo`n $6$G9JP2GE$FAoZGGQNNycPfGCHq/Ra4MSQNknATMgHLzk8N9FHDefbZm/Hcx6JdV/sZdbkFHVVkoRjTnoUP9mw6CkE/.4fI.
^C
我哪里出错了?
【问题讨论】:
-
但是,在一般情况下,您有两个可能无限的
while循环。你确定他们的终止条件受到了影响吗?我还建议将其分解为几个子功能,而不是一个巨大的单片功能,这样调试起来会容易得多。 -
程序停止,但没有返回,所以当我回到我的电脑时,我必须按 ctrl-c,这没什么大不了的,但它不正确。我很确定它在到达 EOF 或 '' 字符串时会停止。盐的长度最终总会得到满足,从 6 到 16 它只是不断将伪随机字母数字字符添加到列表中,最终总是最多达到 16。我认为这与杀死main() 函数线程,但不一定是程序的执行。我认为 sys.exit() 的一些怪癖,也许我需要捕获 sys.exit() 引发的异常
-
实际上@aruisdante 是正确的,我打印了'at EOF'和
if ln == ''语句,它不断地打印“at EOF”。现在我需要学习如何正确阅读 EOF,谢谢伙计。 -
如果要从文件中读取行,只需使用
with open(...) as f:后跟for line in f:(示例here)。它将在文件末尾自动停止。假设文件在您阅读时没有被写入。当你读完文件时,它也会正确地close。
标签: python return main exit freeze