我需要编写一个打开两个文件的函数,如果某个比较为真,则将结果写入第三个文件:
def crack_pass_file(pass_filename,words_filename,out_filename):
f1=open(pass_filename)
f2=open(words_filename)
f3=open(out_filename,'w')
for line1 in f1:
x=make_dictionary(line1.split(":"))
password=x["password"]
name=x["account"]
for line2 in f2:
if (check_pass(line2.strip(),password)==True):
f3.write(name + "="+line2.strip())
文件f1是一个包含以下内容的文件:
root:VgzdTLne0kfs6:0:0:Corema Latterll:/home/root:/bin/bash
checani:IqAFDoIjL2cDs:1:1:Pengpu Checani:/home/checani:/bin/bash
rkrakow:DLD3nJmCvt3pY:2:2:Rodentia Krakow:/home/rkrakow:/bin/bash
forkland:oWMVyy1FTdNL6:3:3:Forkland Maskins:/home/forkland:/bin/bash
obongo:O44lPEloqk5tY:4:4:Obongo Obwalden:/home/obongo:/bin/bash
pglenda:xboW5dHcsqvSQ:5:5:Pahsien Glenda:/home/pglenda:/bin/bash
madel:qEHvJXMkTSAZA:6:6:Madel Aporosa:/home/madel:/bin/bash
ssauks:Q3Kz1z7eAiwjg:7:7:Schober Sauks:/home/ssauks:/bin/bash
slajoie:wWTHgoE8SC8W6:8:8:Scheiner Lajoie:/home/slajoie:/bin/bash
tieton:RWORYLxRSSzMU:9:9:Lerwa Tieton:/home/tieton:/bin/bash
其中每一行都有一个用户,一个密码和一堆其他东西。对于第1行,root是用户,VgzdTLne0kfs6是加密密码。每隔一行使用相同的格式。我需要做的是获取加密密码,看看是否在另一个文件f2中使用密码调用crypt,其中包含按字母顺序排列的巨大单词列表,如:
embordering
emborders
emboscata
embosk
embosked
embosking
将该密码设为加密密码。 我已经有了这样做的功能,它可以工作:
def check_pass(plain,enc):
s = enc[0:2]
x = crypt.crypt(plain, s)
if x==enc: return True
else: return False
这里也是makedictionary函数,我用来从文件1的每一行创建一个字典,也工作我已经测试过了:
def make_dictionary(s):
d={}
d["account"]=s[0]
d["shell"]=s[6]
d["UID"]=int(s[2])
d["GID"]=int(s[3])
d["GECOS"]=s[4]
d["directory"]=s[5]
d["password"]=s[1]
return d
当我测试它时,它写道: 根= stroam 输出文件,这是第一个用户的解密密码,但没有写任何其余的。这应该是结果:
root=stroam
checani=asarta
rkrakow=sinklike
obongo=yawnful
madel=aviatic
tieton=dagesh
pglenda=sngissa
forkland=relliarb
slajoie=mu2j1k
ssauks=EGaFeIHC
我在单词文件中找到了所有这些单词(yawnful,aviatic等)但由于某种原因它们没有被匹配。我也尝试过这样做: check_pass(" asarta"," IqAFDoIjL2cDs")和其他一些他们都返回true所以我很困惑为什么他们中的更多不打印到我的输出文件。
答案 0 :(得分:0)
您希望评估f1
中f2
中每一行的f2
中的每一行。但是,在f1
的第一次迭代中,您会使用f2
中的每一行。在第二次迭代中,for line1 in f1:
# ...
for line2 in f2:
为空,您将不会进入此循环的主体。
for line in f1
相反,在f2_lines = [line2 for line2 in f2]
...
for line1 in f1:
循环之外,生成f2(以及可选f1)中的行列表,如下所示:
for line2 in f2_lines:
现在你可以使用
f1
在 <my:AuditInformation>
<my:AuditQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>85</my:QuestionID>
<my:QuestionText>Loadout Note No.</my:QuestionText>
<my:QuestionAnswerTextbox>1</my:QuestionAnswerTextbox>
<my:QuestionAnswerValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<my:QuestionTypeID>3</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText>eg: 5AZA123456</my:QuestionTooltipText>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>87</my:QuestionID>
<my:QuestionText>Shipment Number</my:QuestionText>
<my:QuestionAnswerTextbox/>
<my:QuestionAnswerValue>2</my:QuestionAnswerValue>
<my:QuestionTypeID>1</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText/>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>88</my:QuestionID>
<my:QuestionText>Destination Port</my:QuestionText>
<my:QuestionAnswerTextbox>3</my:QuestionAnswerTextbox>
<my:QuestionAnswerValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<my:QuestionTypeID>3</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText/>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>100</my:QuestionID>
<my:QuestionText>Container ID</my:QuestionText>
<my:QuestionAnswerTextbox>56456</my:QuestionAnswerTextbox>
<my:QuestionAnswerValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<my:QuestionTypeID>3</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText>Container ID number</my:QuestionTooltipText>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>101</my:QuestionID>
<my:QuestionText>Container 2</my:QuestionText>
<my:QuestionAnswerTextbox>6</my:QuestionAnswerTextbox>
<my:QuestionAnswerValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<my:QuestionTypeID>3</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText/>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>102</my:QuestionID>
<my:QuestionText>Container 3</my:QuestionText>
<my:QuestionAnswerTextbox>7</my:QuestionAnswerTextbox>
<my:QuestionAnswerValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<my:QuestionTypeID>3</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText/>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
<my:AuditInformationQuestions>
<my:QuestionID>322</my:QuestionID>
<my:QuestionText>Exporting KR, RE, IN</my:QuestionText>
<my:QuestionAnswerTextbox/>
<my:QuestionAnswerValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<my:QuestionTypeID>2</my:QuestionTypeID>
<my:AuditQuestionsYesNoSection>
<my:AuditQuestionYes>false</my:AuditQuestionYes>
<my:AuditQuestionNo>false</my:AuditQuestionNo>
</my:AuditQuestionsYesNoSection>
<my:QuestionTooltipText/>
<my:QuestionAnswerDropList>0</my:QuestionAnswerDropList>
</my:AuditInformationQuestions>
</my:AuditQuestions>
循环的正文中。
答案 1 :(得分:0)
第一次迭代f2
时,你耗尽了迭代器,而f2
的所有未来迭代都是根本不执行的循环。将f2
的所有行加载到list
中,以便可以多次迭代,或者在耗尽后调用f2.seek(0)
以重置文件指针。