10

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?

0

4 Answers 4

11

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())
Sign up to request clarification or add additional context in comments.

3 Comments

Please do not suggest people to use AutoAddPolicy, without explaining the consequences! You are losing protection against Man-in-the-middle attacks! +1 anyway
Also, is you want to download (as per question title), you want get, not put.
@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.
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'

Comments

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)

1 Comment

As I've commented to the answer by @Alex, the method to download is indeed get. So it's not "somehow", it's "obviously".
-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))

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.