Skip to content

Instantly share code, notes, and snippets.

@mikkkee
Created October 20, 2014 08:22
Show Gist options
  • Save mikkkee/4d16028f63a450a886bf to your computer and use it in GitHub Desktop.
Save mikkkee/4d16028f63a450a886bf to your computer and use it in GitHub Desktop.
Download files from a remote VPS and then download it to local machine.
#!/usr/bin/env python
from __future__ import print_function
import sys
import argparse
import shlex
import subprocess
def main(argv):
DOWNLOAD_DIR = 'LOCAL_DOWNLOAD_DIR'
USER = 'REMOTE_USER_NAME'
REMOTE_ADDR = 'REMOTE_HOST_ADDR'
REMOTE_DIR = 'REMOTE_DOWNLOAD_DIR'
parser = argparse.ArgumentParser()
# Urls to download.
parser.add_argument('file',type=str,nargs='+',metavar="filename")
# If present, delete all files in REMOTE_DOWNLOAD_DIR after downloading.
parser.add_argument(
'-c',dest='clean',action='store_true'
)
parser.set_defaults(clean=False)
args = parser.parse_args(argv[1:])
to_download = ' '.join(args.file)
# Cmd to connect to remote host. Use ssh-key for auth.
remote_ssh = ''.join(('ssh ', USER, '@', REMOTE_ADDR))
remote_todo = "{connect} ' {todo}'"
# Download bash cmd in remote host.
remote_download_cmd = 'cd {remote_dir}; for f in {to_download};\
do wget $f;done'.format(
remote_dir=REMOTE_DIR,to_download=to_download
)
# Local scp command to copy files from remote to local machine.
fetch_cmd = 'scp {user}@{remote}:{remote_dir}* {download_dir}'.format(
user=USER,remote=REMOTE_ADDR,
remote_dir=REMOTE_DIR,download_dir=DOWNLOAD_DIR
)
# Remote cmd to delete all files in REMOTE_DOWNLOAD_DIR.
clean_remote_dir = 'rm -r {remote_dir}*'.format(remote_dir=REMOTE_DIR)
# Assembly download and clean cmd.
down_cmd = remote_todo.format(connect=remote_ssh, todo=remote_download_cmd)
del_cmd = remote_todo.format(connect=remote_ssh,todo=clean_remote_dir)
print(shlex.split(down_cmd))
print(shlex.split(fetch_cmd))
p1 = subprocess.Popen(shlex.split(down_cmd))
p1.communicate()
p2 = subprocess.Popen(shlex.split(fetch_cmd))
p2.communicate()
# Clean REMOTE_DOWNLOAD_DIR if set to do so.
if args.clean:
print(shlex.split(del_cmd))
p3 = subprocess.Popen(shlex.split(del_cmd))
p3.communicate()
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment