无法使用paramiko连接到SFTP服务器

问题描述

我正在尝试使用paramiko连接到SFTP服务器。我有一个名为privateDP_withoutPassword.ppk的私钥。由于paramiko不接受.ppk格式的密钥,因此我使用PuTTY密钥生成器创建了RSA私钥,并将其保存在文件Lo.ssh中。然后,我尝试通过以下Python代码连接到SFTP服务器:

sftpkey = paramiko.RSAKey.from_private_key_file("C:\\Users\\User\\Path\\Lo.ssh")
self.transport = paramiko.Transport(sftpservername,sftpport)
self.transport.connect(username = sftpusername,pkey = sftpkey)
self.sftp = paramiko.SFTPClient.from_transport(self.transport)

但是我收到以下输出

DEBUG:paramiko.transport:starting thread (client mode): 0x429dba58
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.7.1
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-3.2.0 SSH OpenVMS V5.5 VMS_sftp_version 3
INFO:paramiko.transport:Connected (version 2.0,client 3.2.0)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group14-sha1','diffie-hellman-group1-sha1'] server key:['ssh-dss','ssh-dss'] client encrypt:['aes128-cbc','3des-cbc','twofish128-cbc','cast128-cbc','twofish-cbc','blowfish-cbc','aes192-cbc','aes256-cbc','twofish192-cbc','twofish256-cbc','arcfour','aes128-ctr','aes192-ctr','aes256-ctr'] server encrypt:['aes128-cbc','aes256-ctr'] client mac:['hmac-sha1','hmac-sha1-96','hmac-md5','hmac-md5-96'] server mac:['hmac-sha1','hmac-md5-96'] client compress:['none','zlib'] server compress:['none','zlib'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group14-sha1
DEBUG:paramiko.transport:HostKey agreed: ssh-dss
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha1
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexGroup14 specified hash_algo <built-in function openssl_sha1>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Attempting public-key auth...
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Auth banner: b'\r\n\n\n   This system is for authorized users only.  All users expressly\n   consent to the monitoring and recording of all activity.  System\n   violations are subject to disciplinary
action.  Evidence of\n   criminal activity may be  provided to local,state or Federal law\n   enforcement agencies.\n\n\n'
INFO:paramiko.transport:Authentication (publickey) Failed.
Traceback (most recent call last):
  File "c:/Users/User/Path/sftp_client.py",line 172,in <module>
    client = sftp_client()
  File "c:/Users/User/Path/sftp_client.py",line 30,in __init__
    self.transport.connect(username = sftpusername,pkey = sftpkey)
  File "C:\Users\User\AppData\Local\Programs\Python\python37\lib\site-packages\paramiko\transport.py",line 1333,in connect
    self.auth_publickey(username,pkey)
  File "C:\Users\User\AppData\Local\Programs\Python\python37\lib\site-packages\paramiko\transport.py",line 1580,in auth_publickey
    return self.auth_handler.wait_for_response(my_event)
  File "C:\Users\User\AppData\Local\Programs\Python\python37\lib\site-packages\paramiko\auth_handler.py",line 250,in wait_for_response
    raise e
paramiko.ssh_exception.AuthenticationException: Authentication Failed.
DEBUG:paramiko.transport:EOF in transport thread

我在做什么错?使用WinSCP或Cyber​​duck的此密钥并生成RSA私钥,我可以毫无问题地连接到SFTP服务器。

这是WinSCP日志:

. 2020-08-20 08:45:04.690 --------------------------------------------------------------------------
. 2020-08-20 08:45:04.690 WinSCP Version 5.17.7 (Build 10640) (OS 10.0.17763 - Windows 10 Enterprise)
. 2020-08-20 08:45:04.690 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2020-08-20 08:45:04.690 Log level: normal
. 2020-08-20 08:45:04.690 Local account: LocalAccount
. 2020-08-20 08:45:04.690 Working directory: C:\Program Files (x86)\WinSCP
. 2020-08-20 08:45:04.690 Process ID: 11072
. 2020-08-20 08:45:04.690 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" 
. 2020-08-20 08:45:04.690 Time zone: Current: GMT+2,Standard: GMT+1 (Central European Standard Time),DST: GMT+2 (Central European Daylight Time),DST Start: 3/29/2020,DST End: 10/25/2020
. 2020-08-20 08:45:04.690 Login time: Thursday,August 20,2020 8:45:04 AM
. 2020-08-20 08:45:04.690 --------------------------------------------------------------------------
. 2020-08-20 08:45:04.690 Session name: User@host.com (Ad-Hoc site)
. 2020-08-20 08:45:04.690 Host name: host.com (Port: 22)
. 2020-08-20 08:45:04.690 User name: User (Password: No,Key file: Yes,Passphrase: No)
. 2020-08-20 08:45:04.690 Tunnel: No
. 2020-08-20 08:45:04.690 Transfer Protocol: SFTP (SCP)
. 2020-08-20 08:45:04.690 Ping type: Off,Ping interval: 30 sec; Timeout: 15 sec
. 2020-08-20 08:45:04.690 disable Nagle: No
. 2020-08-20 08:45:04.690 Proxy: None
. 2020-08-20 08:45:04.690 Send buffer: 262144
. 2020-08-20 08:45:04.690 SSH protocol version: 2; Compression: No
. 2020-08-20 08:45:04.690 Bypass authentication: No
. 2020-08-20 08:45:04.690 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
. 2020-08-20 08:45:04.690 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom: 
. 2020-08-20 08:45:04.690 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2020-08-20 08:45:04.690 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,dh-group1-sha1
. 2020-08-20 08:45:04.690 SSH Bugs: Auto,Auto,Auto
. 2020-08-20 08:45:04.690 Simple channel: Yes
. 2020-08-20 08:45:04.690 Return code variable: Autodetect; Lookup user groups: Auto
. 2020-08-20 08:45:04.690 Shell: default
. 2020-08-20 08:45:04.690 EOL: LF,UTF: Auto
. 2020-08-20 08:45:04.690 Clear aliases: Yes,Unset nat.vars: Yes,Resolve symlinks: Yes; Follow directory symlinks: No
. 2020-08-20 08:45:04.690 LS: ls -la,Ign LS warn: Yes,Scp1 Comp: No; Exit code 1 is error: No
. 2020-08-20 08:45:04.690 SFTP Bugs: Auto,Auto
. 2020-08-20 08:45:04.690 SFTP Server: default
. 2020-08-20 08:45:04.690 Local directory: default,Remote directory: home,Update: Yes,Cache: Yes
. 2020-08-20 08:45:04.690 Cache directory changes: Yes,Permanent: Yes
. 2020-08-20 08:45:04.690 Recycle bin: Delete to: No,Overwritten to: No,Bin path: 
. 2020-08-20 08:45:04.690 DST mode: Win
. 2020-08-20 08:45:04.690 --------------------------------------------------------------------------
. 2020-08-20 08:45:04.752 Looking up host "host.com" for SSH connection
. 2020-08-20 08:45:04.837 Connecting to xxx.xxx.xxx.xxx port 22
. 2020-08-20 08:45:05.091 We claim version: SSH-2.0-WinSCP_release_5.17.7
. 2020-08-20 08:45:05.342 Remote version: SSH-2.0-3.2.0 SSH OpenVMS V5.5 VMS_sftp_version 3
. 2020-08-20 08:45:05.342 Using SSH protocol version 2
. 2020-08-20 08:45:05.344 Have a kNown host key of type dss
. 2020-08-20 08:45:05.559 Doing Diffie-Hellman key exchange using 2048-bit modulus and hash SHA-1 with standard group "group14"
. 2020-08-20 08:45:06.038 Host key fingerprint is:
. 2020-08-20 08:45:06.038 ssh-dss 1024 ee:aa:dd:88:66:ff:ee:97:16:7a:7c:52:76:c5:ca:99 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
. 2020-08-20 08:45:06.081 Host key matches cached key
. 2020-08-20 08:45:06.083 Initialised AES-256 SDCTR (AES-NI accelerated) outbound encryption
. 2020-08-20 08:45:06.083 Initialised HMAC-SHA-1 outbound MAC algorithm
. 2020-08-20 08:45:06.152 Initialised AES-256 SDCTR (AES-NI accelerated) inbound encryption
. 2020-08-20 08:45:06.152 Initialised HMAC-SHA-1 inbound MAC algorithm
. 2020-08-20 08:45:06.519 Reading key file "C:\Users\User\Path\privateDP_withoutPassword.ppk"
! 2020-08-20 08:45:06.522 Using username "User".
! 2020-08-20 08:45:07.284    This system is for authorized users only.  All users expressly
! 2020-08-20 08:45:07.284    consent to the monitoring and recording of all activity.  System
! 2020-08-20 08:45:07.284    violations are subject to disciplinary action.  Evidence of
! 2020-08-20 08:45:07.284    criminal activity may be  provided to local,state or Federal law
! 2020-08-20 08:45:07.284    enforcement agencies.
. 2020-08-20 08:45:09.583 Server offered these authentication methods: publickey
. 2020-08-20 08:45:09.583 Offered public key
. 2020-08-20 08:45:09.860 Offer of public key accepted
! 2020-08-20 08:45:09.860 Authenticating with public key "rsa-key-20200221"
. 2020-08-20 08:45:10.056 Sent public key signature
. 2020-08-20 08:45:10.289 Access granted
. 2020-08-20 08:45:10.289 opening main session channel
. 2020-08-20 08:45:10.506 Opened main channel
. 2020-08-20 08:45:10.753 Started a shell/command
. 2020-08-20 08:45:10.807 --------------------------------------------------------------------------

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)