代码:
pkey=paramiko.RSAKey.from_private_key_file(key_path)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host, username='xxxxx',pkey=pkey)
我没有给私钥设置任何密码。所以,我没有提出任何论据。
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 435, in connect
self._auth(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 766, in _auth
raise saved_exception
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 736, in _auth
key = self._key_from_filepath(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/client.py", line 588, in _key_from_filepath
key = klass.from_private_key_file(key_path, password)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/pkey.py", line 249, in from_private_key_file
key = cls(filename=filename, password=password)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/rsakey.py", line 64, in __init__
self._from_private_key_file(filename, password)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/rsakey.py", line 194, in _from_private_key_file
data = self._read_private_key_file("RSA", filename, password)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/pkey.py", line 322, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/pkey.py", line 351, in _read_private_key
data = self._read_private_key_openssh(lines[start:end], password)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/paramiko/pkey.py", line 452, in _read_private_key_openssh
raise PasswordRequiredException(
paramiko.ssh_exception.PasswordRequiredException: private key file is encrypted
我能够成功地使用FileZilla和SSH连接。
SSH详细输出
ssh -v -i /Users/aptuz/Downloads/integra_rsa user@host
OpenSSH_8.6p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/aptuz/.ssh/config
debug1: /Users/aptuz/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to host port 22.
debug1: Connection established.
debug1: identity file /Users/aptuz/Downloads/integra_rsa type 0
debug1: identity file /Users/aptuz/Downloads/integra_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.6
debug1: Remote protocol version 2.0, remote software version GXSSSHD_Comments
debug1: compat_banner: no match: GXSSSHD_Comments
debug1: Authenticating to host:22 as 'user'
debug1: load_hostkeys: fopen /Users/aptuz/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: ecdh-sha2-nistp256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256-etm@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256-etm@openssh.com compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-rsa SHA256:9spuefLNoQEYjtHhNfF3Emwbnlqibxbam2QYf5VXEp4
debug1: load_hostkeys: fopen /Users/aptuz/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'host' is known and matches the RSA host key.
debug1: Found key in /Users/aptuz/.ssh/known_hosts:10
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 4294967296 blocks
debug1: Will attempt key: /Users/aptuz/Downloads/integra_rsa RSA SHA256:A8qNBO5MTleOBw3oRxo09zj5iiLpG6Tl8u0iHnG+7W0 explicit
debug1: SSH2_MSG_SERVICE_ACCEPT received
Please login with user ID and password.debug1: Authentications that can continue: password,publickey,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/aptuz/Downloads/integra_rsa RSA SHA256:A8qNBO5MTleOBw3oRxo09zj5iiLpG6Tl8u0iHnG+7W0 explicit
debug1: Server accepts key: /Users/aptuz/Downloads/integra_rsa RSA SHA256:A8qNBO5MTleOBw3oRxo09zj5iiLpG6Tl8u0iHnG+7W0 explicit
debug1: Authentication succeeded (publickey).
Authenticated to host ([xxx.xxx.xxx.xxx]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem full
debug1: Sending environment.
debug1: channel 0: setting env LANG = "en_US.UTF-8"
This server does not support an interactive session.
Goodbye.
debug1: channel 0: free: client-session, nchannels 1
Connection to host closed.
Transferred: sent 3688, received 2716 bytes, in 1.6 seconds
Bytes per second: sent 2302.4, received 1695.6
debug1: Exit status -1
发布于 2022-08-29 06:30:38
从堆栈跟踪中,您可以看到错误不是来自您的RSAKey.from_private_key_file
,而是来自于SSHClient.connect
中的某个地方。
这可能意味着使用key_path
的身份验证失败,Paramiko不得不尝试.ssh
文件夹中的默认密钥(因为SSHClient.connect
的默认look_for_keys=True
参数)。你可能那里有一个加密的密钥。没有加载它,隐藏您假定的“身份验证失败”问题。
查看Paramiko日志文件以获得详细信息。你可能有这样的问题:
https://stackoverflow.com/questions/73518110
复制相似问题