Python Script to Transfer Windows C Drive Files to Server (Paramiko Installation)
To transfer all files from the C drive of a Windows system to a server using Python, you can use the following code snippet:
import paramiko
import os
# Define the host, username, and password for the server
host = 'your_server_address'
username = 'your_username'
password = 'your_password'
# Create an SSH client object
ssh = paramiko.SSHClient()
# Automatically add the server to the known_hosts file
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the server
ssh.connect(host, username=username, password=password)
# Define the local and remote paths
local_path = 'C:/'
remote_path = '/home/your_username/transfer_files/'
# Create a list of files in the local directory
files = os.listdir(local_path)
# Loop through each file and transfer it to the remote directory
for file_name in files:
local_file_path = os.path.join(local_path, file_name)
remote_file_path = os.path.join(remote_path, file_name)
ssh.exec_command('mkdir -p ' + remote_path)
sftp = ssh.open_sftp()
sftp.put(local_file_path, remote_file_path)
sftp.close()
# Close the SSH connection
ssh.close()
To install paramiko, you can use pip, which is the package installer for Python. Open the command prompt and enter the following command:
pip install paramiko
This will install the latest version of paramiko. Alternatively, you can download the source code from the official website and install it manually.
原文地址: https://www.cveoy.top/t/topic/lDHW 著作权归作者所有。请勿转载和采集!