- 1 year ago
How do I make my file copy script using pscp write logs?
Comment
1 / 1
To make your file copy script using pscp
write logs, you can redirect the output of pscp
to a log file. Here's a sample script in Python that demonstrates how to achieve this:
pythonimport os
import subprocess
def copy_files(source_path, destination_path, hostname, username, password, log_file):
try:
# Construct the pscp command
pscp_command = f'pscp -pw {password} -r {source_path} {username}@{hostname}:{destination_path}'
# Run pscp command and redirect the output to the log file
with open(log_file, 'w') as log:
subprocess.run(pscp_command, shell=True, stdout=log, stderr=log)
print("File copy successful!")
except Exception as e:
print(f"File copy failed: {e}")
if __name__ == "__main__":
# Replace these values with your actual parameters
source_path = "C:/path/to/source/files"
destination_path = "/path/to/destination/folder"
hostname = "your_remote_server_ip_or_hostname"
username = "your_remote_server_username"
password = "your_remote_server_password"
log_file = "copy_log.txt"
copy_files(source_path, destination_path, hostname, username, password, log_file)
This Python script utilizes the subprocess
module to run the pscp
command and redirect the output to a log file named copy_log.txt
. You can customize the source_path
, destination_path
, hostname
, username
, password
, and log_file
variables to suit your specific use case.
When you run the script, it will copy the files from the specified source directory to the destination directory on the remote server using pscp
, and the output of the pscp
command will be written to the copy_log.txt
file.