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

2024-09-26 21:41:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过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,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-256,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,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@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,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: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,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-sha256,diffie-hellman-group-exchange-sha256,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,publickey,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,keyboard-interactive
debug3: start over, passed a different list password,keyboard-interactive
debug3: preferred publickey,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.

我尝试使用PySTFP,但结果证明,它只是Paramiko上的一个包装器,不支持MFA。现在,如果我理解正确的话,帕拉米科在默认情况下可以使用MFA。即使不是这样,我也尝试从头开始构建我的传输对象,但得到了相同的异常

出什么事了


Tags: keyenvcomparamikopackettypepasswordhmac
1条回答
网友
1楼 · 发布于 2024-09-26 21:41:31

我提出的解决方案是在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)

相关问题 更多 >

    热门问题