1

Python 3.3 on RHEL 6.

With the code below, if I get the password right the first time, it correctly prints 'authenticated', then returns me to my shell. If I get the password wrong 3 times it correctly prints 'Too many tries, sorry.', then returns me to my shell. But if I get the password wrong once or twice, then get it right, it prints 'authenticated', but then just hangs there and does not send me back to my shell. I have to press CTRL-C to break out of the script.

Any ideas?

import paramiko
import getpass
authenticated, tries = False, 1
while authenticated == False and tries <= 3:
    try:
        password = getpass.getpass('Password: ')
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(myhost, username=myusername, password=password)
        authenticated = True
    except paramiko.AuthenticationException:
        authenticated = False
        tries += 1
if authenticated == False:
    print('Too many tries, sorry.')
else:
    print('Authenticated')
1
  • Works for me on python 2.x linux. Likely unrelated, but try closing the ssh connection at the end of the script, even when connect fails - paramiko runs background threads and maybe that has something to do with it. Commented Apr 14, 2014 at 20:41

1 Answer 1

1

As tdelaney said, you need to add ssh.close(). Tested using Python 3.3:

import paramiko
import getpass
authenticated, tries = False, 1
myhost     = 'myserver.mydomain.org'
myusername = 'myuser'
while authenticated == False and tries <= 3:
    try:
        password = getpass.getpass('Password: ')
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(myhost, username=myusername, password=password)
        authenticated = True
    except paramiko.AuthenticationException:
        authenticated = False
        tries += 1
if authenticated == False:
    print('Too many tries, sorry.')
else:
    print('Authenticated')
ssh.close()

Without ssh.close() the script hangs when I input one wrong password and then the right password. It doesn't hangs otherwise (2 wrong password and then the right password is ok, it doesn't hang).

You can see the active threads if you import threading and at the last line print('Threads:', threading.enumerate()) :

import paramiko
import getpass
import threading

authenticated, tries = False, 1
myhost     = 'myserver.mydomain.org'
myusername = 'myuser'
while authenticated == False and tries <= 3:
    try:
        password = getpass.getpass('Password: ')
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(myhost, username=myusername, password=password)
        authenticated = True
    except paramiko.AuthenticationException:
        authenticated = False
        tries += 1
if authenticated == False:
    print('Too many tries, sorry.')
else:
    print('Authenticated')
#ssh.close()
print('Threads:', threading.enumerate())

When I tested it, it printed after one wrong password and the right password :

Threads: [<_MainThread(MainThread, started 140298690832128)>, <paramiko.Transport at 0x14e3a90 (cipher aes128-ctr, 128 bits) (connected; awaiting auth)>, <paramiko.Transport at 0x147a910 (cipher aes128-ctr, 128 bits) (active; 0 open channel(s))>]

I know this doesn't really explain why it does that, but I hope it helps to solve your problem and see what's happening.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.