Created
February 14, 2018 22:07
-
-
Save igorcosta/2ae19434d3550bcc658dc738d98c7b0f to your computer and use it in GitHub Desktop.
SFTP with Python using paramiko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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