用于从该框内通过 SSH 连接到 jumphost 和 sftp 的 Python 脚本 [英] Python script to SSH into a jumphost and sftp from within that box

查看:71
本文介绍了用于从该框内通过 SSH 连接到 jumphost 和 sftp 的 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试编写一个气流作业,它允许我通过 ssh 进入一个 EC2 实例,然后从这个 EC2 框内与另一个主机开始一个 sftp 会话.我目前拥有的代码如下:

I'm currently trying to write an airflow job that will allow me to ssh into an EC2 instance and then start an sftp session with another host from within this EC2 box. My current code that I have is as follows:

def run_ssh():
    hook = SSHHook(ssh_conn_id='xyz').get_conn() #returns an ssh client
    stdin, stdout, stderr = hook.exec_command('sftp user@host.com;')
    # This next step prompts me for password so i provide it
    stdin.write('password')
    logging.info(stdout.readlines())
    stdin, stdout, stderr = hook.exec_command('ls')
    logging.info(stdout.readlines())

当我打印最后一行时,我应该会看到一些文件夹,而只是看到 ['a\n']...所以看起来我实际上无法 sftp.是否有更好的方法通过本地运行的 python 脚本从远程主机 sftp.

When i print the final line i should be seeing some folders but instead just see ['a\n']... so it seems I'm not actually able to sftp. Are there better ways to sftp from a remote host through a python script running locally.

对此的任何帮助表示赞赏.答案可以针对一个简单的 python 脚本,而不是气流.

Any help with this is appreciated. The answer can be geared towards a simple python script as opposed to airflow.

推荐答案

有关您的文字问题,请参阅:
使用 Python Paramiko 通过 SSH 将输入/变量传递给命令/脚本

For your literal question, see:
Pass input/variables to command/script over SSH using Python Paramiko

尽管以这种方式通过跳转主机实现 SFTP 并不是一个好的解决方案.

Though implementing an SFTP over jump host this way is not a good solution.

改用端口转发:

这篇关于用于从该框内通过 SSH 连接到 jumphost 和 sftp 的 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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