2014-04-14 5 views
1

파이썬 3.3 내가 암호를 한 번에 제대로을받을 경우 '인증', 제대로 인쇄 아래 코드와Paramiko은 (파이썬 3)

RHEL 6에 예외 후 프롬프트를 반환하지 않은, 다음에 나를 돌려 내 껍질. 비밀 번호를 3 번 ​​잘못 입력하면 '너무 많이 시도합니다. 죄송합니다.'라는 메시지가 제대로 표시되면 저를 제 셸로 되돌립니다. 하지만 암호를 한 두 번 잘못 입력 한 다음 올바르게 가져 오면 '인증'된 내용이 인쇄됩니다. 그런 다음 잠시 멈추고 다시 내 셸로 보내지 않습니다. 나는 CTRL-C를 눌러 스크립트를 빠져 나가야한다.

아이디어가 있으십니까?

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') 
+0

나를 위해 Python 2.x Linux에서 작동합니다. 관계가 없지만, 스크립트가 끝날 때 ssh 연결을 닫으려고 시도하십시오. 연결이 실패하더라도 -paramiko는 백그라운드 스레드를 실행하고 아마도 그 작업과 관련이 있습니다. – tdelaney

답변

1

tdelaney가 말한대로 ssh.close()을 추가해야합니다. 파이썬 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() 

ssh.close()없이 스크립트가 I 입력 한 잘못된 암호 다음 오른쪽 비밀번호를 중단됩니다. 그렇지 않으면 (2 잘못된 암호 및 오른쪽 암호를 확인, 응답하지 않습니다) 그렇지 않으면 응답하지 않습니다.

import threading 당신이 경우 마지막 줄 print('Threads:', threading.enumerate())에서 당신은 활성 스레드를 볼 수 있습니다

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))>] 

: 나는 그것을 시험 할 때, 그것은 하나의 잘못된 암호 및 올바른 암호를 한 후 인쇄

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()) 

나는 이것이 이것이 왜 그렇게하는지 설명하지 않는다는 것을 안다. 그러나 나는 당신의 문제를 해결하고 무슨 일이 일어나고 있는지를 알기를 희망한다.