Skip to content

Instantly share code, notes, and snippets.

@igorcosta
Created February 14, 2018 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igorcosta/2ae19434d3550bcc658dc738d98c7b0f to your computer and use it in GitHub Desktop.
Save igorcosta/2ae19434d3550bcc658dc738d98c7b0f to your computer and use it in GitHub Desktop.
SFTP with Python using paramiko
#!/usr/bin/env python
import sys
import time
import paramiko
## How to use it?
##
## You have to install a dependecy called paramiko, which is a ssh protocol implementation that helps you to connect to sftp.
## pip install paramiko
## Commands in your terminal:
##
## file_to_sftp.py [domain] [port] [account] [password] [remote_path] [local_file]
## sftp.py sftp.loyalty.com 22 ACCOUNT PASSWORD folder/dest.zip /tmp/vicky.zip
if len(sys.argv) != 7:
print 'error param'
exit()
try:
fn, host, port, account, password, dest, src = sys.argv
transport = paramiko.Transport((host, int(port)))
transport.connect(username=account, password=password)
sftp = paramiko.SFTPClient.from_transport(transport)
arr = dest.split('/')
if len(arr) > 1:
dir = '/'.join(arr[0:len(arr)-1])
sftp.mkdir(dir)
sftp.chdir(dir)
sftp.put(src, '/'.join(arr[len(arr)-1:]))
else:
sftp.put(src, dest)
sftp.close()
print 'done'
except:
print 'error'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment