使用 Python 通过 SSH 下载文件 [英] Download files over SSH using Python

查看:103
本文介绍了使用 Python 通过 SSH 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个通过 ssh 下载(或上传)文件的脚本,因为防火墙禁用了 ftp 端口.这是我的脚本:

I am trying to make a script that downloads ( or upload ) files over ssh, as ftp port is disabled from firewall. This is my script :

import os
import paramiko 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.170.21.93', username="abhishek", password="@bhishek$")
sftp = ssh.open_sftp()
localpath = 'abc.txt'
remotepath = '/opt/crestelsetup/patchzip'
sftp.put(localpath, remotepath)
sftp.close()
ssh.close()

这给了我IOError:失败",有人可以帮忙吗?

This is giving me "IOError: Failure", can any one help?

推荐答案

需要明确指定远程路径:

You need to explicitly specify the remote path:

import os
import paramiko 
ssh = paramiko.SSHClient()
ssh.connect('10.170.21.93', username="abhishek", password="@bhishek$")
sftp = ssh.open_sftp()
localpath = 'abc.txt'
remotepath = '/opt/crestelsetup/patchzip/abc.txt'
sftp.put(localpath, remotepath)
sftp.close()
ssh.close()

根据 Martin Prikryl 的评论,以下代码行是非常不鼓励的,因为它可以让您在中间攻击中对抗人,但是,它可以是丢失主机密钥的临时修复

As per Martin Prikryl's comment, the following code line is highly discouraged as it opens you up against man in the middle attack, however, it can be a temporary fix for missing host keys

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

这篇关于使用 Python 通过 SSH 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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