Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Paramiko进行多因素身份验证(密码和密钥)_Python_Ssh_Sftp_Paramiko - Fatal编程技术网

Python 使用Paramiko进行多因素身份验证(密码和密钥)

Python 使用Paramiko进行多因素身份验证(密码和密钥),python,ssh,sftp,paramiko,Python,Ssh,Sftp,Paramiko,我有以下代码: import paramiko policy = paramiko.client.WarningPolicy() client = paramiko.client.SSHClient() client.set_missing_host_key_policy(policy) username = '...' password = '...' file_path = '...' pkey = paramiko.RSAKey.from_private_key_file(file_pat

我有以下代码:

import paramiko
policy = paramiko.client.WarningPolicy()
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(policy)
username = '...'
password = '...'
file_path = '...'
pkey = paramiko.RSAKey.from_private_key_file(file_path)
client.connect('...', username=username, password=password, pkey=key)
sftp = client.open_sftp() 
从文件上看,它似乎应该起作用。一切都成功运行,但当代码点击
客户端时,open_sftp
会弹出一个
SSHException:无法打开频道。
并且传输(从
客户端.获取传输
)处于活动状态,但未经过身份验证。为此启用调试日志记录时也遇到问题(我正在尝试
logging.getLogger('paramiko').setLevel(logging.debug)
,但没有成功。)


关于从哪里开始调试这个非常模糊的错误消息,有什么想法吗?

在您声明的脚本中

pkey = paramiko.RSAKey.from_private_key_file(file_path)
然后,您使用的不是pkey,而是
pkey=key


不确定密钥来自何处,但这可能是一个问题。

很抱歉反应太晚,但很难找到有关此问题的任何信息,因此我想为其他陷入此问题的人发布一个解决方案

在努力解决这个问题之后,多亏了Doug Ellwanger和Daniel Brownridge发布的一些代码,我找到了一个解决方案。这个问题似乎是由多因素身份验证的处理方式引起的,使用的是更多的交互方式

import paramiko
import threading

... 

username = '...'
password = '...'
file_path = '...'
pkey = paramiko.RSAKey.from_private_key_file(file_path)
sftpClient = multifactor_auth('...', 22, username, pkey, password)

...

def multifactor_auth_sftp_client(host, port, username, key, password):
    #Create an SSH transport configured to the host
    transport = paramiko.Transport((host, port))
    #Negotiate an SSH2 session
    transport.connect()
    #Attempt authenticating using a private key
    transport.auth_publickey(username, key)
    #Create an event for password auth
    password_auth_event = threading.Event()
    #Create password auth handler from transport
    password_auth_handler = paramiko.auth_handler.AuthHandler(transport)
    #Set transport auth_handler to password handler
    transport.auth_handler = password_auth_handler
    #Aquire lock on transport
    transport.lock.acquire()
    #Register the password auth event with handler
    password_auth_handler.auth_event = password_auth_event
    #Set the auth handler method to 'password'
    password_auth_handler.auth_method = 'password'
    #Set auth handler username
    password_auth_handler.username = username
    #Set auth handler password
    password_auth_handler.password = password
    #Create an SSH user auth message
    userauth_message = paramiko.message.Message()
    userauth_message.add_string('ssh-userauth')
    userauth_message.rewind()
    #Make the password auth attempt
    password_auth_handler._parse_service_accept(userauth_message)
    #Release lock on transport
    transport.lock.release()
    #Wait for password auth response
    password_auth_handler.wait_for_response(password_auth_event)
    #Create an open SFTP client channel
    return transport.open_sftp_client()

我希望这对我的项目有帮助。

Paramiko通常可以自己正确处理(至少在大多数情况下)。不需要如此复杂的解决方案。对于一些例子或一个罕见的情况,当Paramiko失败时,请参阅-尽管在这个答案中有比低级代码更简单的解决方案。谢谢,它对我有用。为了使您的答案完整,还要在代码中添加“导入线程”。