Loading

This article describes a small project that grew out of a need to copy files from one secure server to another. If you need to transfer a file from one computer to another, you might think of sending it via email, USB drive, or cloud storage. However, sometimes, these methods may not be feasible or secure.

One option is to use a Python script that copies a file from one computer’s folder to another, using a username and password to access the share. In this article, we will walk you through creating such a script.

First, we need to install the necessary Python modules. We will use the smbprotocol module to connect to the SMB (Server Message Block) share and the click module to handle the command-line interface.

You can install these modules using pip, the Python package installer, by running the following command:

pip install smbprotocol click

Once the modules are installed, we can start writing our Python script.

import smbprotocol
import click

@click.command()
@click.option("--source", required=True, help="The source file path (including the filename) on the remote computer.")
@click.option("--destination", required=True, help="The destination file path (including the filename) on the local computer.")
@click.option("--username", required=True, help="The username to access the remote share.")
@click.option("--password", required=True, help="The password to access the remote share.")
@click.option("--host", required=True, help="The hostname or IP address of the remote computer.")
@click.option("--share", required=True, help="The name of the remote share.")
def copy_file(source, destination, username, password, host, share):
    # Connect to the SMB share
    conn = smbprotocol.SMBConnection(host, username=username, password=password)
    conn.connect()

    # Open the remote file
    with conn.open_file(share, source, smbprotocol.FileAccessMask.GENERIC_READ) as remote_file:
        # Create the local file
        with open(destination, "wb") as local_file:
            # Copy the remote file to the local file
            local_file.write(remote_file.read())

    # Disconnect from the SMB share
    conn.disconnect()

Let’s go through the script step-by-step:

  • First, we import the necessary modules (smbprotocol and click).
  • We define a command-line interface using the click module. The @click.command() decorator defines a new command, and the @click.option() decorators define the command’s options (i.e., the parameters that the user can specify).
  • The copy_file() function is the main function of the script. It takes six arguments (source, destination, username, password, host, and share) corresponding to the command-line options.
  • We create an SMB connection to the remote computer inside the function using the smbprotocol.SMBConnection() method. We pass the host, username, and password arguments to authenticate the connection.
  • We open the remote file using the conn.open_file() method. We pass the share and source arguments to specify the file path on the remote computer. We also specify the smbprotocol.FileAccessMask.GENERIC_READ access mask to read the file.
  • We create the local file using the open() function. We pass the destination argument to specify the file path on the local computer. We also specify the “wb” mode to open the file in binary write mode.
  • We copy the contents of the remote file to the local file using the local_file.write(remote_file.read()) statement.
  • Finally, we disconnect from the SMB share using the article…

Now that we have written the script, we can run it from the command-line. Assuming that we have saved the script as copy_file.py, we can run it with the following command:

python copy_file.py --source //remote_computer/share/path/to/file --destination /local/path/to/file --username user --password pass --host remote_computer --share share

Here, we specify the source file path on the remote computer using the –source option, the destination file path on the local computer using the –destination option, the username and password to access the remote share using the –username and –password options, the hostname or IP address of the remote computer using the –host option, and the name of the remote share using the –share option.

Note that the file paths on Windows-based remote computers are specified using the backslash (\) character, an escape character in Python. We can use the forward slash (/) character or escape the backslash (\\) character to avoid errors.

In conclusion, copying files between computers can be a hassle, especially if you need to transfer sensitive or large files. Using a Python script that copies a file from one computer’s folder to another, and using a username and password to access the share, can save you time and effort. The smbprotocol module provides a simple and secure way to connect to SMB shares, and the click module makes it easy to define a command-line interface. With a few lines of code, you can create a tool that automates the file transfer process and makes managing your files easier.

Add Comment

Your email address will not be published. Required fields are marked *