Created
July 25, 2017 13:33
-
-
Save diopib/0801b3b6cd47802e12067f650004d05e to your computer and use it in GitHub Desktop.
Securely copy a file from A to B using SSH
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
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