无法同时使用 .openssh 密钥和密码通过 Paramiko 连接到 SFTP

如何解决无法同时使用 .openssh 密钥和密码通过 Paramiko 连接到 SFTP

我正在尝试通过 Python + Paramiko 设置连接到 SFTP 服务器。 SFTP 服务器需要 MFA。我可以通过 CLI 连接,没有像这样的问题:

sftp -vvv -i key.openssh -oPreferredAuthentications=publickey,password User@xxx.yyy.com

结果如下:

OpenSSH_8.1p1,LibreSSL 2.7.3
debug1: Reading configuration data /Users/user/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to xxx.yyy.com port 22.
debug1: Connection established.
debug1: identity file /Users/user/.ssh/key.openssh type -1
debug1: identity file /Users/user/.ssh/key.openssh-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0,remote software version 1.82_sshlib Globalscape
debug1: no match: 1.82_sshlib Globalscape
debug3: fd 5 is O_NONBLOCK
debug1: Authenticating to xxx.yyy.com:22 as 'User'
debug3: hostkeys_foreach: reading file "/Users/user/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /Users/user/.ssh/known_hosts:8
debug3: load_hostkeys: loaded 1 keys from xxx.yyy.com
debug3: order_hostkeyalgs: prefer hostkeyalgs: rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: rsa-sha2-512-cert-v01@openssh.com,ssh-rsa,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519
debug2: ciphers ctos: chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
debug2: ciphers stoc: chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
debug2: MACs ctos: umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: umac-64-etm@openssh.com,hmac-sha1
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: diffie-hellman-group16-sha512,diffie-hellman-group14-sha1
debug2: host key algorithms: ssh-rsa
debug2: ciphers ctos: aes256-ctr,aes128-ctr
debug2: ciphers stoc: aes256-ctr,aes128-ctr
debug2: MACs ctos: hmac-sha2-512,hmac-sha2-256
debug2: MACs stoc: hmac-sha2-512,hmac-sha2-256
debug2: compression ctos: zlib,none
debug2: compression stoc: zlib,none
debug2: languages ctos:
debug2: languages stoc:
debug2: first_kex_follows 0
debug2: reserved 0
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug3: send packet: type 34
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<8192<8192) sent
debug3: receive packet: type 31
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug2: bits set: 978/2048
debug3: send packet: type 32
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug3: receive packet: type 33
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:******************
debug3: hostkeys_foreach: reading file "/Users/user/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /Users/user/.ssh/known_hosts:8
debug3: load_hostkeys: loaded 1 keys from xxx.yyy.com
debug3: hostkeys_foreach: reading file "/Users/user/.ssh/known_hosts"
debug3: record_hostkey: found key type RSA in file /Users/user/.ssh/known_hosts:8
debug3: load_hostkeys: loaded 1 keys from **.***.***.**
debug1: Host 'xxx.yyy.com' is known and matches the RSA host key.
debug1: Found key in /Users/user/.ssh/known_hosts:8
debug2: bits set: 1040/2048
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 4294967296 blocks
debug1: Will attempt key: /Users/user/.ssh/key.openssh RSA SHA256:***************** agent
debug1: Will attempt key: /Users/user/.ssh/key.openssh  explicit
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 53
debug3: input_userauth_banner
EFT Login - %DATE% %TIME% - Please enter valid credentials to continuedebug3: receive packet: type 51
debug1: Authentications that can continue: password,publickey,keyboard-interactive
debug3: start over,passed a different list password,keyboard-interactive
debug3: preferred publickey,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/user/.ssh/key.openssh RSA SHA256:********************** agent
debug3: send packet: type 50
debug2: we sent a publickey packet,wait for reply
debug3: receive packet: type 60
debug1: Server accepts key: /Users/user/.ssh/key.openssh RSA SHA256:**********************agent
debug3: sign_and_send_pubkey: 
RSA SHA256:**********************
debug3: sign_and_send_pubkey: signing using ssh-rsa
debug3: send packet: type 50
debug3: receive packet: type 51
Authenticated with partial success.
debug1: Authentications that can continue: password,password
debug3: authmethod_lookup password
debug3: remaining preferred:,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
User@xxx.yyy.com's password:

<here I input password> 


debug3: send packet: type 50
debug2: we sent a password packet,wait for reply
debug3: receive packet: type 52
debug1: Authentication succeeded (password).
Authenticated to xxx.yyy.com ([**.***.***.**]:22).
debug2: fd 6 setting O_NONBLOCK
debug3: fd 7 is O_NONBLOCK
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug3: send packet: type 90
debug1: Entering interactive session.
debug1: pledge: network
debug3: receive packet: type 91
debug2: channel_input_open_confirmation: channel 0: callback start
debug2: fd 5 setting TCP_NODELAY
debug3: ssh_packet_set_tos: set IP_TOS 0x20
debug2: client_session2_setup: id 0
debug1: Sending environment.
debug3: Ignored env SHELL
debug3: Ignored env LSCOLORS
debug3: Ignored env PYENV_HOOK_PATH
debug3: Ignored env ITERM_PROFILE
debug3: Ignored env COLORTERM
debug3: Ignored env PYENV_SHELL
debug3: Ignored env LESS
debug3: Ignored env XPC_FLAGS
debug3: Ignored env TERM_PROGRAM_VERSION
debug3: Ignored env _P9K_TTY
debug3: Ignored env JAVA_HOME
debug3: Ignored env __CFBundleIdentifier
debug3: Ignored env SSH_AUTH_SOCK
debug3: Ignored env P9K_TTY
debug3: Ignored env TERM_SESSION_ID
debug3: Ignored env EDITOR
debug3: Ignored env PYENV_VERSION
debug3: Ignored env PWD
debug3: Ignored env LOGNAME
debug3: Ignored env LaunchInstanceID
debug3: Ignored env FZF_DEFAULT_COMMAND
debug3: Ignored env COMMAND_MODE
debug3: Ignored env ITERM_SESSION_ID
debug3: Ignored env HOME
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env LS_COLORS
debug3: Ignored env SECURITYSESSIONID
debug3: Ignored env TMPDIR
debug1: Sending env LC_TERMINAL = iTerm2
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env CLICOLOR
debug3: Ignored env PYENV_DIR
debug3: Ignored env ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR
debug3: Ignored env TERM
debug3: Ignored env USER
debug3: Ignored env COLORFGBG
debug3: Ignored env VISUAL
debug1: Sending env LC_TERMINAL_VERSION = 3.4.9beta1
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env DISPLAY
debug3: Ignored env SHLVL
debug3: Ignored env PAGER
debug3: Ignored env XPC_SERVICE_NAME
debug1: Sending env LC_CTYPE = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env PYENV_ROOT
debug3: Ignored env P9K_SSH
debug3: Ignored env HOMEBREW_NO_INSTALL_CLEANUP
debug1: Sending env LC_ALL = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: send packet: type 98
debug3: Ignored env PATH
debug3: Ignored env OLDPWD
debug3: Ignored env __CF_USER_TEXT_ENCODING
debug3: Ignored env TERM_PROGRAM
debug3: Ignored env PYXTERM_DIMENSIONS
debug3: Ignored env LINES
debug3: Ignored env JUPYTER_SERVER_ROOT
debug3: Ignored env JUPYTER_SERVER_URL
debug3: Ignored env COLUMNS
debug3: Ignored env _
debug1: Sending subsystem: sftp
debug2: channel 0: request subsystem confirm 1
debug3: send packet: type 98
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 33554432 rmax 35840
debug3: receive packet: type 99
debug2: channel_input_status_confirm: type 99 id 0
debug2: subsystem request accepted on channel 0
debug2: Remote version: 3
Connected to xxx.yyy.com.
debug3: Sent message fd 3 T:16 I:1
debug3: SSH_FXP_REALPATH . -> / size 0

但是当尝试从 Python 执行相同操作时:

import paramiko
private_key = paramiko.RSAKey.from_private_key_file('key.openssh',password=r'key_password')
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxx.yyy.com',username='User',password="password",pkey=private_key,allow_agent=False,look_for_keys=False)

我结束了

DEBUG:paramiko.transport:starting thread (client mode): 0xd1b36d0
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.2
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-1.82_sshlib Globalscape
INFO:paramiko.transport:Connected (version 2.0,client 1.82_sshlib)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group16-sha512','diffie-hellman-group14-sha256','diffie-hellman-group-exchange-sha256','diffie-hellman-group14-sha1'] server key:['ssh-rsa'] client encrypt:['aes256-ctr','aes128-ctr'] server encrypt:['aes256-ctr','aes128-ctr'] client mac:['hmac-sha2-512','hmac-sha2-256'] server mac:['hmac-sha2-512','hmac-sha2-256'] client compress:['zlib','none'] server compress:['zlib','none'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group16-sha512
DEBUG:paramiko.transport:HostKey agreed: ssh-rsa
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexGroup16SHA512 specified hash_algo <built-in function openssl_sha512>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Trying SSH key b'xxxxxxx'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Auth banner: b'EFT Login - %DATE% %TIME% - Please enter valid credentials to continue'
INFO:paramiko.transport:Authentication continues...
DEBUG:paramiko.transport:Methods: ['password','keyboard-interactive']
INFO:paramiko.transport:Disconnect (code 2): unexpected service request

和例外:

AuthenticationException: Authentication failed.

我尝试使用 pysftp,但结果证明它只是 Paramiko 的包装器,不支持 MFA。如果我理解正确,那么默认情况下,Paramiko 应该可以使用 MFA。即使不是这样,我也尝试从头开始构建我的 Transport 对象,但遇到了同样的异常。

出了什么问题?

解决方法

我想出的解决方案是在 transport.auth_publickey(username=user,key=private_key) 之后添加:

m = paramiko.Message()
m.add_byte(paramiko.common.cMSG_USERAUTH_REQUEST)
m.add_string(self.user)
m.add_string('ssh-connection')
m.add_string('password')
m.add_boolean(False)
m.add_string(self.password)
transport._send_message(m)
paramiko.SFTPClient.from_transport(transport)

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

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res