paramiko:打开和返回 sftp 连接的方法 [英] paramiko: method to open and return an sftp conneciton

查看:69
本文介绍了paramiko:打开和返回 sftp 连接的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个方法,它接受一个 IP、用户名和密码,并返回一个到该服务器的开放 SFTP 连接.这是我的代码,使用 paramiko:

def open_sftp_connection(ip, user, passwd):ssh = SSHClient()ssh.load_system_host_keys()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(ip, 22, 用户, 密码)断言 ssh.get_transport().is_active(), '无法连接到服务器'sftp = ssh.open_sftp()返回 sftp

如果我直接在 python shell 中执行此代码,它工作正常:

<预><代码>>>>ssh = SSHClient()>>>ssh.load_system_host_keys()>>>ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())>>>ssh.connect(ip, 22, 用户, 密码)>>>断言 ssh.get_transport().is_active(), '无法连接到服务器'>>>sftp = ssh.open_sftp()>>>打印 sftp.get_channel().get_transport().is_active()真的

但是当我把它放在一个函数中并调用该函数时,sftp 连接返回断开:

<预><代码>>>>def open_sftp_connection(ip, user, passwd):... ssh = SSHClient()... ssh.load_system_host_keys()... ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())... ssh.connect(ip, 22, 用户, 密码)...断言 ssh.get_transport().is_active(), '无法连接到服务器'... sftp = ssh.open_sftp()...返回sftp...>>>sftp = open_sftp_connection(ip, user, passwd)>>>打印 sftp.get_channel().get_transport().is_active()错误的

我唯一能想到的就是ssh连接本地存储在ssh中,然后当函数返回时,ssh被垃圾回收,连接终止,所以虽然 SFTPClient 仍然存在,但底层连接现在已经死了.

如果是这种情况,我该如何解决?如果不是,实际发生了什么?

EDIT:我修改了函数以返回 sftp, ssh,现在连接保持打开状态.所以垃圾收集器才是罪魁祸首.不返回两个值就可以解决这个问题吗?

解决方案

您需要访问 ssh 和 sftp 实例.

def open_sftp_connection(ip, user, passwd):ssh = SSHClient()ssh.load_system_host_keys()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(ip, 22, 用户, 密码)断言 ssh.get_transport().is_active(), '无法连接到服务器'返回 ssh, ssh.open_sftp()ssh, sftp = open_sftp_connection(ip, user, passwd)打印 sftp.get_channel().get_transport().is_active()

I want to write a method that takes an IP, username, and password, and returns an open SFTP connection to that server. Here's the code I have, using paramiko:

def open_sftp_connection(ip, user, passwd):
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip, 22, user, passwd)
    assert ssh.get_transport().is_active(), 'Failed to connect to server'

    sftp = ssh.open_sftp()
    return sftp

If I execute this code directly in a python shell, it works fine:

>>> ssh = SSHClient()
>>> ssh.load_system_host_keys()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect(ip, 22, user, passwd)
>>> assert ssh.get_transport().is_active(), 'Failed to connect to server'
>>> sftp = ssh.open_sftp()
>>> print sftp.get_channel().get_transport().is_active()
True

But when I put it in a function and call the function, the sftp connection comes back disconnected:

>>> def open_sftp_connection(ip, user, passwd):
...     ssh = SSHClient()
...     ssh.load_system_host_keys()
...     ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
...     ssh.connect(ip, 22, user, passwd)
...     assert ssh.get_transport().is_active(), 'Failed to connect to server'
...     sftp = ssh.open_sftp()
...     return sftp
...
>>> sftp = open_sftp_connection(ip, user, passwd)
>>> print sftp.get_channel().get_transport().is_active()
False

The only thing I can think of is the ssh connection is stored locally in ssh, then when the function returns, ssh is garbage collected, and the connection is terminated, so while the SFTPClient still exists, the underlying connection is now dead.

If that's the case, how can I resolve it? If not, what is actually happening?

EDIT: I modified the function to return sftp, ssh and now the connection stays open. So it is the garbage collector that's the culprit. Can this be resolved without returning two values?

解决方案

You need access to both the ssh and sftp instances.

def open_sftp_connection(ip, user, passwd):
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(ip, 22, user, passwd)
    assert ssh.get_transport().is_active(), 'Failed to connect to server'
    return ssh, ssh.open_sftp()

ssh, sftp = open_sftp_connection(ip, user, passwd)
print sftp.get_channel().get_transport().is_active()

这篇关于paramiko:打开和返回 sftp 连接的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆