Last active
September 8, 2022 17:20
-
-
Save Zeitsperre/448f8d6d7bf907c9e9976b4bf2069fb1 to your computer and use it in GitHub Desktop.
Using SCP in Python
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 logging | |
from datetime import datetime as dt | |
from getpass import getpass | |
from pathlib import Path | |
from paramiko import SSHClient | |
from scp import SCPClient | |
logging.basicConfig( | |
filename=f"{ dt.strftime(dt.now(), '%Y-%m-%d')}_{Path(__file__).stem}.log", | |
level=logging.INFO, | |
) # can't go wrong making a logfile | |
user = "my_user_name" # username on the server being accessed. | |
my_folder = Path("/to/my/folder/") # folder being transferred | |
with SSHClient() as ssh: | |
ssh.load_system_host_keys() # loads any SSH keys. If keys are loaded, password not needed. | |
server_address = f"server.host.ca" | |
logging.info(f"Connecting to {server_address}") | |
pw = getpass("Password: ") # prompt for password | |
ssh.connect(server_address, username=user, password=pw) # opens a shell to create a connection. | |
logging.info(f"Connected!") | |
with SCPClient(ssh.get_transport(), socket_timeout=30.0) as scp: | |
scp.put(my_folder, recursive=True, remote_path='/home/user/whatever') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment