Skip to content

Instantly share code, notes, and snippets.

@xorenio
Created February 16, 2023 16:15
Show Gist options
  • Save xorenio/795b03bf2eb729a664dd4cfbda9d648b to your computer and use it in GitHub Desktop.
Save xorenio/795b03bf2eb729a664dd4cfbda9d648b to your computer and use it in GitHub Desktop.
Uploading files from windows
// Prompt for openAI's ChatGPT: Make python script for windows to upload given folder to ssh
import os
import paramiko
local_path = r'C:\path\to\folder'
remote_path = '/path/on/remote/server'
hostname = 'remote.server.com'
username = 'your_username'
password = 'your_password'
# Connect to the remote server
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username, password=password)
# Create the remote directory if it does not exist
stdin, stdout, stderr = ssh.exec_command(f'mkdir -p {remote_path}')
print(stdout.read().decode())
# Upload the files to the remote server
sftp = ssh.open_sftp()
for root, dirs, files in os.walk(local_path):
for file in files:
local_file_path = os.path.join(root, file)
remote_file_path = os.path.join(remote_path, os.path.relpath(local_file_path, local_path))
sftp.put(local_file_path, remote_file_path)
sftp.close()
# Close the SSH connection
ssh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment