Search code examples
pythonexceptionprintingpasswordscracking

Error exception causing print to fail - python2


Here is a snippet from my code. For some reason it simply won't print out the second line saying "Cracking took 10 seconds" or whatever, but this first bit saying Password Found: does work... Why?

def connect(host, user, password, release):
    global Found
    global Fails
    global startTime

    try:
        s = pxssh.pxssh()
        s.login(host, user, password)
        print '[+] Password Found: ' + password
        print 'Cracking the password took' + datetime.now()-startTime + 'seconds.'
    Found = True

    except Exception, e:
        if 'read_nonblocking' in str(e):
        Fails += 1
            time.sleep(5)
            connect(host, user, password, False)
    elif 'synchronize with original prompt' in str(e):
        time.sleep(1)
        connect(host, user, password, False)

Solution

  • You are trying to concatenate two different things (datetime and str), try converting the datetime to str as:

    def connect(host, user, password, release):
        global Found
        global Fails
        global startTime
    
        try:
            s = pxssh.pxssh()
            s.login(host, user, password)
            print '[+] Password Found: ' + password
            print 'Cracking the password took' + str(datetime.now()-startTime) + 'seconds.'
            Found = True
    
        except Exception, e:
            if 'read_nonblocking' in str(e):
                Fails += 1
                time.sleep(5)
                connect(host, user, password, False)
            elif 'synchronize with original prompt' in str(e):
                time.sleep(1)
                connect(host, user, password, False)
    

    Moreover, you shouldn't trap all kind Exception, just those you need.