Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jaymzcd/957066 to your computer and use it in GitHub Desktop.

Select an option

Save jaymzcd/957066 to your computer and use it in GitHub Desktop.
zipAndTrans.py
#!/usr/bin/env python2
import os
import sys
import paramiko
import zipfile
local_file = sys.argv[1]
KNOWN_HOSTS = os.path.join(os.getenv("HOME"), '.ssh', 'known_hosts')
CONFIG = dict(host='example.com', username='root', password='passwd')
TARGET_PATH = '/tmp/'
path, filename = os.path.split(local_file)
filename, ext = os.path.splitext(filename)
local_zip = os.path.join(path, filename+'.zip')
zip_file = zipfile.ZipFile(local_zip, 'w')
zip_file.write(local_file)
zip_file.close()
ssh = paramiko.SSHClient()
ssh.load_host_keys(KNOWN_HOSTS)
ssh.connect(CONFIG['host'], username=CONFIG['username'], password=CONFIG['password'])
sftp = ssh.open_sftp()
sftp.put(local_file, TARGET_PATH+filename+ext)
sftp.put(local_zip, TARGET_PATH+filename+'.zip')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment