"No hostkey for ... found" 在 pysftp 代码中,即使 cnopts.hostkeys 设置为 None
"No hostkey for ... found" in pysftp code even though cnopts.hostkeys is set to None
背景
要通过 SFTP 连接到另一台服务器,我在 UNIX 命令行中使用以下命令:
sftp -i /some_dir/another_dir/key -oPort=12345 user@12.123.456.789
我想达到的目标
我想将其转换为与 PySFTP
一起使用的命令。
我试过的
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# source: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html
srv = pysftp.Connection("user@12.123.456.789", port="12345",
private_key_pass="/some_dir/another_dir/key")
遇到错误
File "./aws_sql_dumper.py", line 14, in <module>
srv = pysftp.Connection("user@12.123.456.789", port="12345", private_key_pass="/some_dir/another_dir/key")
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host user@12.123.456.789 found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x7f6067c7ea20>>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 1013, in __del__
self.close()
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
研究完成
我已阅读以下内容:
- pysftp 0.2.9 breaks builds
- Welcome to pysftp’s documentation!
- Stuck in a pysftp Dilemma - Needing Help
- S/O - Verify host key with pysftp
问题
我认为我做错了根本性的事情。 UNIX命令行中使用的SFTP命令如何被pysftp
采用accepted/interpretted?
变化
我改变了一些东西
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load('/home/some_dir/.ssh/known_hosts')
# source: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html
srv = pysftp.Connection("user@12.123.456.789", port="12345",
private_key="/some_dir/another_dir/key", cnopts=cnopts)
改变后的输出
Traceback (most recent call last):
File "./aws_sql_dumper.py", line 17, in <module>
srv = pysftp.Connection("user@12.123.456.789", port="12345", private_key="/some_dir/another_dir/key", cnopts=cnopts)
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host user@12.123.456.789" found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x7f8120dc6438>>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 1013, in __del__
self.close()
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live
您从不使用 cnopts
变量。您需要将其传递给 Connection
class.
的 cnopts
参数
解决此问题后,您将遇到私钥问题,因为您将其传递给 private_key_pass
参数(“私钥密码”)而不是至 private_key
.
srv = pysftp.Connection("user@12.123.456.789", port="12345",
private_key="/some_dir/another_dir/key", cnopts=cnopts)
尽管您不应该这样做 cnopts.hostkeys = None
,因为这样做会失去安全感。正确的解决方案是验证主机密钥。检查 my answer to the same question 以获得正确的解决方案。
经过反复试验,将以下SFTP命令转换成终端:
sftp -i /some_dir/another_dir/key -oPort=12345 user@12.123.456.789
可以翻译*为:
paramiko.SSHClient().connect(hostname='12.123.456.789', username='user', port=12345,
key_filename='/some_dir/another_dir/key')
整个压缩代码是:
#!/usr/bin/python3
import paramiko
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(hostname='12.123.456.789', username='user', port=12345,
key_filename='/some_dir/another_dir/key')
# -------------------------- [ just for testing ] --------------------------
stdin, stdout, stderr = client.exec_command('ls -la') # THIS IS FOR TESTING
print(stdout.read()) # AND PRINTING OUT
finally:
client.close()
我遇到了类似的问题(主机密钥问题)并找到了一个很好的解决方案。我正在尝试从 windows 框(源)连接到 Linux 框(目标)。在我的 windows 机器上,我们已经安装了 CYGWIN,我在其中登录并执行了以下
1. Logged in using my ID
2. Ran the command ssh-keygen -b 2048 -t rsa
3. under my cygwin home dir (~/.ssh) folder was created with pub and private key.
4. Physically this folder would be in c:\cygwin\home\/.ssh
5. Copy this folder and paste in the c:\user\.ssh (windows home dir)
6. also open the pub key in the folder and paste that in the destination server ~\.ssh\authroized_keys. This is for password less login.
在此之后 pysftp 无需密码即可连接。将毫无问题地检测到主机密钥
-Prasanna.K
背景
要通过 SFTP 连接到另一台服务器,我在 UNIX 命令行中使用以下命令:
sftp -i /some_dir/another_dir/key -oPort=12345 user@12.123.456.789
我想达到的目标
我想将其转换为与 PySFTP
一起使用的命令。
我试过的
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# source: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html
srv = pysftp.Connection("user@12.123.456.789", port="12345",
private_key_pass="/some_dir/another_dir/key")
遇到错误
File "./aws_sql_dumper.py", line 14, in <module>
srv = pysftp.Connection("user@12.123.456.789", port="12345", private_key_pass="/some_dir/another_dir/key")
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host user@12.123.456.789 found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x7f6067c7ea20>>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 1013, in __del__
self.close()
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
研究完成
我已阅读以下内容:
- pysftp 0.2.9 breaks builds
- Welcome to pysftp’s documentation!
- Stuck in a pysftp Dilemma - Needing Help
- S/O - Verify host key with pysftp
问题
我认为我做错了根本性的事情。 UNIX命令行中使用的SFTP命令如何被pysftp
采用accepted/interpretted?
变化
我改变了一些东西
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys.load('/home/some_dir/.ssh/known_hosts')
# source: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html
srv = pysftp.Connection("user@12.123.456.789", port="12345",
private_key="/some_dir/another_dir/key", cnopts=cnopts)
改变后的输出
Traceback (most recent call last):
File "./aws_sql_dumper.py", line 17, in <module>
srv = pysftp.Connection("user@12.123.456.789", port="12345", private_key="/some_dir/another_dir/key", cnopts=cnopts)
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host user@12.123.456.789" found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x7f8120dc6438>>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 1013, in __del__
self.close()
File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live
您从不使用 cnopts
变量。您需要将其传递给 Connection
class.
cnopts
参数
解决此问题后,您将遇到私钥问题,因为您将其传递给 private_key_pass
参数(“私钥密码”)而不是至 private_key
.
srv = pysftp.Connection("user@12.123.456.789", port="12345",
private_key="/some_dir/another_dir/key", cnopts=cnopts)
尽管您不应该这样做 cnopts.hostkeys = None
,因为这样做会失去安全感。正确的解决方案是验证主机密钥。检查 my answer to the same question 以获得正确的解决方案。
经过反复试验,将以下SFTP命令转换成终端:
sftp -i /some_dir/another_dir/key -oPort=12345 user@12.123.456.789
可以翻译*为:
paramiko.SSHClient().connect(hostname='12.123.456.789', username='user', port=12345,
key_filename='/some_dir/another_dir/key')
整个压缩代码是:
#!/usr/bin/python3
import paramiko
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(hostname='12.123.456.789', username='user', port=12345,
key_filename='/some_dir/another_dir/key')
# -------------------------- [ just for testing ] --------------------------
stdin, stdout, stderr = client.exec_command('ls -la') # THIS IS FOR TESTING
print(stdout.read()) # AND PRINTING OUT
finally:
client.close()
我遇到了类似的问题(主机密钥问题)并找到了一个很好的解决方案。我正在尝试从 windows 框(源)连接到 Linux 框(目标)。在我的 windows 机器上,我们已经安装了 CYGWIN,我在其中登录并执行了以下
1. Logged in using my ID 2. Ran the command ssh-keygen -b 2048 -t rsa 3. under my cygwin home dir (~/.ssh) folder was created with pub and private key. 4. Physically this folder would be in c:\cygwin\home\/.ssh 5. Copy this folder and paste in the c:\user\.ssh (windows home dir) 6. also open the pub key in the folder and paste that in the destination server ~\.ssh\authroized_keys. This is for password less login.
在此之后 pysftp 无需密码即可连接。将毫无问题地检测到主机密钥
-Prasanna.K