我要做的就是让程序查找特定的字符串,如果文件包含该字符串,则将其添加到变量中,如果不是,则仅打印它不起作用。但是我一直说,当我知道它在文件中时,找不到该字符串。
for line in open("/home/cp/Desktop/config"):
if "PermitRootLogin no" in line:
num_of_points1 = num_of_points1 + 4
num_of_vulns1 = num_of_vulns1 + 1
else:
print('sshd_config is still vulnerable')
break
这是其读取的文件
This should work
Possibly
PermitRootLogin no
hmmmmmmmmmm
aaa
我想要它做的是在文件中找到“ PermitRootLogin no”,但它一直像从未找到过的字符串那样工作。并打印else语句。当我在寻找教程时,他们都在尝试做其他事情,因此我愿意接受任何建议。谢谢高级即时消息,我真的很受困扰。
嗨,你的问题是在其他情况下休息。您的代码读取第一行,并输出第一行不是您搜索的字符串。下面的代码对我有用。希望这可以帮助。
found = False
for line in open('config'):
if "PermitRootLogin no" in line:
num_of_points1 = num_of_points1 + 4
num_of_vulns1 = num_of_vulns1 + 1
found = True
if not found:
print('sshd_config is still vulnerable')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句