9

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()

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

Abhishek dot py
  • 909
  • 3
  • 15
  • 31

4 Answers4

9

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()

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())
Alex
  • 21,273
  • 10
  • 61
  • 73
  • 4
    Please do not suggest people to use `AutoAddPolicy`, without explaining the consequences! You are losing protection against [Man-in-the-middle attacks](https://en.wikipedia.org/wiki/Man-in-the-middle_attack)! +1 anyway – Martin Prikryl Mar 21 '17 at 10:18
  • 2
    Also, is you want to **download** (as per question title), you want `get`, **not** `put`. – Martin Prikryl Mar 07 '19 at 10:42
  • @MartinPrikryl I have edited my answer regarding AutoAddPolicy. Unfortunately in real life most administrators do not provide the host key fingerprint and they can be subject to change (hardware upgrades, etc). It makes storing host keys a maintenance overhead, especially when deploying Dockerised solutions. Of course It all depends on the risk of a potential man in the middle attack and there are scenarios where you absolutely do want to verify host keys. – Alex Mar 07 '19 at 12:28
1

Just modified the destination path to include the file name as well.Try to change.

remotepath = '/opt/crestelsetup/patchzip'

to

remotepath = '/opt/crestelsetup/patchzip/abc.txt'
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0

Somehow put method was not working for me but this get method works fine

localpath = "Test.txt"
remotepath = "/root/test/Test.txt"
sftp.get(remotepath,localpath)
  • As I've commented to the answer by @Alex, the method to download is indeed `get`. So it's not *"somehow"*, it's *"obviously"*. – Martin Prikryl Aug 22 '23 at 05:58
-1

You need to modify remotepath. Since, your remote path is /opt/crestelsetup/patchzip. Now need to upload file join with remote path. It can be done using following way.

fname = os.path.basename(localpath)
sftp.put(localpath, os.path.join(remotepath, fname))
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Paran
  • 11
  • 3
  • You cannot use `os.path.join` with an SFTP path. SFTP path always uses *forward slashes*, while `os.path.join` will use path separators of the local system (so for example backslashes on Windows). – Martin Prikryl Oct 31 '19 at 07:57