Skip to content

Instantly share code, notes, and snippets.

@diopib
Created July 25, 2017 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diopib/0801b3b6cd47802e12067f650004d05e to your computer and use it in GitHub Desktop.
Save diopib/0801b3b6cd47802e12067f650004d05e to your computer and use it in GitHub Desktop.
Securely copy a file from A to B using SSH
import paramiko
from scp import SCPClient
__author__ = "ibrahim@sikilabs.com"
"""
Example script: file scp
requirements:
- paramiko==2.2.1
- scp==0.10.2
"""
def sshcopy(host, username, keyfile, from_path, to_path):
"""
simply securely copy a file from A to remote B
:param host: B hostname or IP
:param username: B username
:param keyfile: SSH private key file to connect to B
:param from_path: absolute path of file in A to copy
:param to_path: absolute path of folder in B to copy into
:return: None
"""
client = paramiko.SSHClient()
pkey = paramiko.RSAKey.from_private_key(keyfile)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hotsname=host, username=username, pkey=pkey)
cp = SCPClient(client.get_transport())
cp.put(from_path, to_path)
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment