0

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)
Coder77
  • 2,203
  • 5
  • 20
  • 28
  • care to share the exception? – Saeid Mar 03 '14 at 20:31
  • None, that is my issue. I am guessing the exception is causing it to go wrong. I am unsure though. – Coder77 Mar 03 '14 at 20:32
  • It may be because `datetime.now()-startTime` isn't a string, try with `str(datetime.now()-startTime)`. Also `Found = True` doesn't have a correct identation – Ruben Bermudez Mar 03 '14 at 20:32
  • The Found = True does have correct indentation, stackoverflow just messed it up. Just to point out I am running this within the commandline as it requires extra arguements (-t for example) – Coder77 Mar 03 '14 at 20:38
  • If adding `str(...)` doesn't fix the issue, remove try-except and update your question with the traceback, that will help to find a solution – Ruben Bermudez Mar 03 '14 at 20:41
  • `str()` did not work, and I received this error `cannot concatenate 'str' and 'datetime.datetime' objects` – Coder77 Mar 03 '14 at 20:42

2 Answers2

1

The issue is probably that you haven't set startTime, but you masked it by over-broad exception handling. Either remove the try/except, select some other exception to trap, or simply include a bare raise command in your exception handler and you should see a NameError because of the absence of initialization. Fix that and your code has more of a chance.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • I am getting this error `cannot concatenate 'str' and 'datetime.datetime' objects` – Coder77 Mar 03 '14 at 20:41
  • In which your true error is revealed, as pointed out by Ruben above. Exception handling has to be carefully managed to ensure you don't trap and ignore spurious errors. – holdenweb Mar 03 '14 at 21:02
1

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.

Ruben Bermudez
  • 2,293
  • 14
  • 22