Paramiko / Python:键盘交互式身份验证

我在尝试使用Paramiko(Python)创建SFTP客户端时遇到了困难.

码:

 import paramiko as sftp

 transport = sftp.Transport(('myhost',port),default_max_packet_size=10000,default_window_size=10000)
 transport.connect(username='myuser',password='mypassword')
 client_from_transport = sftp.SFTPClient.from_transport(transport)

错误:

   

Traceback (most recent call last):
    File "sftp.py",line 91,in <module>
    sftp_client = create_sftp_client()
    File "...\sftp.py",line 63,in create_sftp_client
    client_from_transport = sftp.SFTPClient.from_transport(transport)
    File "...\Python\Python37\Lib\site-packages\paramiko\sftp_client.py",line 165,in from_transport
    window_size=window_size,max_packet_size=max_packet_size
    File "...\Python\Python37\Lib\site-packages\paramiko\transport.py",line 
    806,in open_session
    timeout=timeout,File "...\Python\Python37\Lib\site-packages\paramiko\transport.py",line 
    933,in open_channel
    raise e
    File "...\Python\Python37\Lib\site-packages\paramiko\transport.py",line 
    1982,in run
    ptype,m = self.packetizer.read_message()
    File "...\Python\Python37\Lib\site-packages\paramiko\packet.py",line 
    441,in read_message
    header = self.read_all(self.__block_size_in,check_rekey=True)
    File "...\Python\Python37\Lib\site-packages\paramiko\packet.py",line 
    290,in read_all
    raise EOFError()
    EOFError

打印运输对象显示:

<paramiko.Transport at 0x68d0c1d0 (cipher aes128-ctr,128 bits) (connected; awaiting auth)>

Paramiko日志文件:

DEB [20190403-12:31:01.550] thr=1   paramiko.transport: starting thread (client mode): 0xfbc42780
DEB [20190403-12:31:01.550] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.4.2
DEB [20190403-12:31:01.567] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-Server
INF [20190403-12:31:01.567] thr=1   paramiko.transport: Connected (version 2.0,client Server)
DEB [20190403-12:31:01.571] thr=1   paramiko.transport: kex algos:['ecdh-sha2-nistp521','ecdh-sha2-nistp384','ecdh-sha2-nistp256','diffie-hellman-group-exchange-sha256','diffie-hellman-group-exchange-sha1','diffie-hellman-group18-sha512','diffie-hellman-group17-sha512','diffie-hellman-group16-sha512','diffie-hellman-group15-sha512','diffie-hellman-group14-sha256','diffie-hellman-group14-sha1','diffie-hellman-group1-sha1'] server key:['ssh-rsa'] client encrypt:['aes128-ctr','aes192-ctr','aes256-ctr','arcfour256','arcfour128','aes128-cbc','3des-cbc','blowfish-cbc','aes192-cbc','aes256-cbc'] server encrypt:['aes128-ctr','aes256-cbc'] client mac:['hmac-md5','hmac-sha1','hmac-sha2-256','hmac-sha2-512','hmac-sha1-96','hmac-md5-96'] server mac:['hmac-md5','hmac-md5-96'] client compress:['none','zlib','zlib@openssh.com'] server compress:['none','zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: Kex agreed: ecdh-sha2-nistp256
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: HostKey agreed: ssh-rsa
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20190403-12:31:01.572] thr=1   paramiko.transport: Compression agreed: none
DEB [20190403-12:31:01.654] thr=1   paramiko.transport: kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEB [20190403-12:31:01.669] thr=1   paramiko.transport: Switch to new keys ...
DEB [20190403-12:31:01.670] thr=2   paramiko.transport: Attempting password auth...
DEB [20190403-12:31:01.689] thr=1   paramiko.transport: userauth is OK
INF [20190403-12:31:02.010] thr=1   paramiko.transport: Authentication continues...
DEB [20190403-12:31:02.010] thr=1   paramiko.transport: Methods: ['keyboard-interactive']
DEB [20190403-12:31:02.010] thr=2   paramiko.transport: [chan 0] Max packet in: 10000 bytes
DEB [20190403-12:31:02.026] thr=1   paramiko.transport: EOF in transport thread

但是SFTP可在FileZilla中使用:

2019-04-03 13:02:36 13796 1 Status: Connecting to xxxxxx...
2019-04-03 13:02:36 13796 1 Trace: CControlSocket::SendNextCommand()
2019-04-03 13:02:36 13796 1 Trace: CSftpDeleteOpData::Send() in state 0
2019-04-03 13:02:36 13796 1 Trace: Going to execute C:\Program Files\FileZilla FTP Client\fzsftp.exe
2019-04-03 13:02:36 13796 1 Response: fzSftp started,protocol_version=8
2019-04-03 13:02:36 13796 1 Trace: CSftpDeleteOpData::ParseResponse() in state 0
2019-04-03 13:02:36 13796 1 Trace: CControlSocket::SendNextCommand()
2019-04-03 13:02:36 13796 1 Trace: CSftpDeleteOpData::Send() in state 3
2019-04-03 13:02:36 13796 1 Command: open "user" 2222
2019-04-03 13:02:36 13796 1 Trace: Connecting to ip port 2222
2019-04-03 13:02:36 13796 1 Trace: We claim version: SSH-2.0-FileZilla_3.41.2
2019-04-03 13:02:36 13796 1 Trace: Server version: SSH-2.0-Server
2019-04-03 13:02:36 13796 1 Trace: Using SSH protocol version 2
2019-04-03 13:02:36 13796 1 Trace: Doing ECDH key exchange with curve nistp256 and hash SHA-256
2019-04-03 13:02:36 13796 1 Trace: Host key fingerprint is:
2019-04-03 13:02:36 13796 1 Trace: ssh-rsa 1024 xxxx=
2019-04-03 13:02:36 13796 1 Trace: Initialised AES-256 SDCTR client->server encryption
2019-04-03 13:02:36 13796 1 Trace: Initialised HMAC-SHA-256 client->server MAC algorithm
2019-04-03 13:02:36 13796 1 Trace: Initialised AES-256 SDCTR server->client encryption
2019-04-03 13:02:36 13796 1 Trace: Initialised HMAC-SHA-256 server->client MAC algorithm
2019-04-03 13:02:36 13796 1 Trace: Attempting keyboard-interactive authentication
2019-04-03 13:02:36 13796 1 Trace: Using keyboard-interactive authentication. inst_len: 0,num_prompts: 1
2019-04-03 13:02:36 13796 1 Command: Pass: ********
2019-04-03 13:02:36 13796 1 Trace: Access granted
2019-04-03 13:02:36 13796 1 Trace: Opening session as main channel
2019-04-03 13:02:37 13796 1 Trace: Opened main channel
2019-04-03 13:02:37 13796 1 Trace: Started a shell/command
2019-04-03 13:02:37 13796 1 Status: Connected to xxxx

尝试使用sftp_client = SSHClient()而不是通过传输对象创建SFTP客户端时,出现了相同的错误.在创建我的传输对象时也尝试添加timeout = timeout,没有帮助.

请问对此有何想法?

最佳答案
您的服务器正在使用键盘交互式身份验证,而不是简单的密码身份验证.

通常,当密码验证失败并且键盘交互提示仅包含一个字段(可能是密码)时,Paramiko足够聪明,可以退回到键盘交互验证.

问题是您的服务器的行为就像密码验证成功一样.

您可以使用以下代码明确使Paramiko尝试键盘交互式身份验证:

def handler(title,instructions,fields):
    if len(fields) > 1:
        raise SSHException("Expecting one field only.")
    return [password]

transport.connect(username='myuser')
transport.auth_interactive(username,handler)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


本文适合有 Python 基础的小伙伴进阶学习 作者:pwwang 一、前言 本文基于开源项目: https://github.com/pwwang/python-import-system 补充扩展讲解,希望能够让读者一文搞懂 Python 的 import 机制。 1.1 什么是 import
前言 目前有个python应用需要在容器镜像内拉取git私有仓库的代码,一开始的想法是用GitPython,折腾一番ssh私钥和known_hosts问题后,发现还是在镜像中封装个git最省事,然后用subprocess调用系统命令,镜像体积也没有想象中增加特别多。 准备ssh私钥和known_ho
前言 当网络不稳定或应用页面加载有问题,可以设置等待,避免网络问题导致找不到元素等异常。 隐式等待 隐式等待设置的是最长等待时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间结束。 隐式等待在driver的整个生命周期都有效,初始化的时候设置一次即可。 # 隐式等待10秒 drive
前言 map()、reduce()、filter()是python的三个高阶函数。所谓高阶函数,指的是将函数作为参数并返回函数作为结果的函数。下面代码的sing_ready只是一个简单高阶函数示例: def ready(name): return f&quot;ready,{name}!&quot;
入门使用 # 示例代码 warframe = [&quot;saryn&quot;, &quot;wisp&quot;, &quot;volt&quot;] counts = [len(n) for n in warframe] for i,j in zip(warframe,counts): pr
前言 功能描述:批量重命名指定目录下的文件,文件名加前缀,默认格式为“目录名_原文件名”。 示例代码 import argparse import os import sys import logging def gen_args(): &quot;&quot;&quot; 说明 解析命令行参数 &
前言 常见的应用配置方式有环境变量和配置文件,对于微服务应用,还会从配置中心加载配置,比如nacos、etcd等,有的应用还会把部分配置写在数据库中。此处主要记录从环境变量、.env文件、.ini文件、.yaml文件、.toml文件、.json文件读取配置。 ini文件 ini文件格式一般如下: [
前言 在设计API返回内容时,通常需要与前端约定好API返回响应体内容的格式。这样方便前端进行数据反序列化时相应的解析处理,也方便其它服务调用。不同公司有不同的响应内容规范要求,这里以常见的JSON响应体为例: { &quot;code&quot;: 200, &quot;data&quot;: {
前言 我们一般使用如下方式点击元素: elem = driver.find_element(...) elem.click() # 或者使用带等待条件的方式 elem = WebDriverWait(driver, 10).until(EC.xxx(...)) elem.click() 正常情况下,
前言 从环境变量和配置文件中获取配置参数,相关库: python-dotenv:第三方库,需要使用pip安装 configparser:标准库 示例代码 test.ini [mysql] host = &quot;192.168.0.10&quot; port = 3306 user = &quot
前言 Relative Locators,相对定位器,是Selenium 4引入的一个新的定位器,相对定位器根据源点元素去定位相对位置的其它元素。 相对定位方法其实是基于JavaScript的 getBoundingClientRect() 而实现,简单的页面还行,复杂页面中可能会定位到需要相同类型
简介 The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and librari
简介 Faker库可用于随机生成测试用的虚假数据。 可生成的数据参考底部的参考链接。 安装: python -m pip install faker 快速入门 from faker import Faker # 实例化一个对象,本地化使用中国 fk - Faker(locale=&quot;zh_C
前言 原本应用的日志是全部输出到os的stdout,也就是控制台输出。因其它团队要求也要保留日志文件,便于他们用其他工具统一采集,另一方面还要保留控制台输出,便于出问题的时候自己直接看pod日志。具体需求如下: 日志支持同时控制台输出和文件输出 控制台的输出级别可以高点,比如WARNING,个人这边
按列从多个文件中构建 假设有两个csv文件,列不相同,需要整合为一个dataframe,使用glob模块: from glob import glob import pandas as pd # glob会返回任意排序的文件名,所以需要sort排序 some_files = sorted(glob(
简介 diagrams是python的一个第三方库,用于实现使用代码绘制架构图。 安装 依赖于 Graphviz,安装diagrams之前需要先安装 Graphviz(下载压缩包后,将bin目录添加到系统环境变量Path里即可)。 python3 -m pip install diagrams 快速
前言 最近有个个人需求是要把多个图片文件合并为一个PDF文件,这样方便用PDF阅读器连续看,避免界面点一下,只会图片放大。(比如看漫画) 主要思路是先把单张图片转换成单个PDF文件,然后把PDF文件进行合并。原先是用WPS的转换工具做的,但WPS每次只能批量转换30张,如果有大量图片文件,用WPS就
前言 版本: python:3.9 selenium:4.1.5 获取元素文本 text = driver.find_element(by=By.XPATH, value=&quot;&quot;).text 获取元素属性值 attr1 = driver.find_element(by=By.XPA
Python中有个内置的函数叫做 enumerate,可以在迭代时返回元素的索引。 # 示例代码01 warframe = [&quot;saryn&quot;, &quot;wisp&quot;, &quot;volt&quot;] for i,name in enumerate(warframe
前言 版本: python:3.9 selenium:4.1.5 浏览器:firefox 创建浏览器对象 from selenium import webdriver driver = webdriver.Firefox(executable_path=r&quot;C:\software\sele