Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files over SSH using Python

Tags:

python

ssh

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?

like image 352
Abhishek dot py Avatar asked Apr 08 '15 07:04

Abhishek dot py


2 Answers

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())
like image 188
Alex Avatar answered Sep 23 '22 11:09

Alex


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'
like image 32
itzMEonTV Avatar answered Sep 22 '22 11:09

itzMEonTV