python - Paramiko 在异常后不返回提示(Python 3)

标签 python python-3.3 paramiko

RHEL 6 上的 Python 3.3。

使用下面的代码,如果我第一次获得正确的密码,它会正确打印“已验证”,然后返回到我的 shell。如果我输入的密码错误 3 次,它会正确打印“太多次尝试,抱歉。”,然后返回到我的 shell。但是,如果我一次或两次输错密码,然后改正,它会打印“已验证”,但然后只是卡在那里,不会将我送回 shell。我必须按 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')

最佳答案

正如tdelaney所说,您需要添加ssh.close()。使用 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()

如果没有ssh.close(),当我输入一个错误的密码然后输入正确的密码时,脚本会挂起。否则它不会挂起(两次错误的密码,然后正确的密码就可以了,它不会挂起)。

如果导入线程并在最后一行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())

当我测试它时,它在一个错误的密码和正确的密码后打印:

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

我知道这并不能真正解释为什么会这样做,但我希望它有助于解决您的问题并看看发生了什么。

关于python - Paramiko 在异常后不返回提示(Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069612/

相关文章:

python - 在python中每隔n个项目拆分一个生成器/可迭代(splitEvery)

python - 如何解释 keras "predict_generator "输出?

python - Python 中函数的数学积分

python - 属性错误 : 'module' object has no attribute 'request'

Python请求模块不使用post方法

python - 通过 python paramiko 使用本地参数在 ssh 连接上执行受限远程脚本

Python 连接列表的错误结果

python - 如何用任何小数舍入一个值

python - 使用 Paramiko 设置 SSH 隧道以访问 PostgreSQL

python - 如何增加 Paramiko SSH 客户端上的向后滚动缓冲区?