gue*_*tli 5 python ssh paramiko pysftp
该代码不起作用:
def sftp_connection(self):
import pysftp
connection = pysftp.Connection(self.host, username=self.system_name,
private_key=os.path.join(HOME, '.ssh', 'id_rsa'))
# in the next lines I try to use AutoAddPolicy
client = connection.sftp_client()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
return connection
Run Code Online (Sandbox Code Playgroud)
这是例外:
def sftp_connection(self):
import pysftp
connection = pysftp.Connection(self.host, username=self.system_name,
private_key=os.path.join(HOME, '.ssh', 'id_rsa'))
# in the next lines I try to use AutoAddPolicy
client = connection.sftp_client()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
return connection
Run Code Online (Sandbox Code Playgroud)
在尝试设置之前,我得到了例外host_key_policy
。
我找不到通过pysftp访问客户端实例的其他方法。
有没有办法AutoAddPolicy
在我得到异常之前进行设置?
还有一个相关的问题。我的问题是关于如何应用旧问题中提供的几种解决方案之一。
pysftp根本不使用Paramiko SSHClient
类,它使用了更底层的Transport
类。因此它不具有的MissingHostKeyPolicy
功能SSHClient
。
您将必须自己实施。
一种可能的实现方式可以是:
host = 'example.com'
# Loads .ssh/known_hosts
cnopts = CnOpts()
hostkeys = None
if cnopts.hostkeys.lookup(host) == None:
print("New host - will accept any host key")
# Backup loaded .ssh/known_hosts file
hostkeys = cnopts.hostkeys
# And do not verify host key of the new host
cnopts.hostkeys = None
with Connection(host, username = user, private_key = pkey, cnopts = cnopts) as sftp:
if hostkeys != None:
print("Connected to new host, caching its hostkey")
hostkeys.add(host, sftp.remote_server_key.get_name(), sftp.remote_server_key)
hostkeys.save(pysftp.helpers.known_hosts())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2476 次 |
最近记录: |