introduction
In the field of network operation and maintenance and automation, the SSH (Secure Shell) protocol is a common means to connect and manage remote servers. Paramiko is a Python library for SSH2 sessions, which supports encryption, authentication, and file transfer functions. Using Paramiko, remote command execution, file upload and download operations can be easily implemented. This article aims to guide newbies in detail how to use Python's Paramiko library to create remote file directories and upload files.
1. Install Paramiko
First, make sure you have the Paramiko library installed. If not installed, it can be done easily via pip:
pip install paramiko
After the installation is complete, import the required modules in the Python script:
import paramiko
2. Create an SSH connection
Before starting any operation, you need to create an SSH client instance and configure relevant parameters. Here is a basic example:
ssh_client = () ssh_client.set_missing_host_key_policy(()) # Automatically add host key
Next, use the server's IP address, port number, username, and password to connect to the remote server:
ssh_client.connect(hostname='your_server_ip', port=22, username='your_username', password='your_password')
3. Create a remote file directory
Once the connection is successful, you can create a file directory by executing the SSH command. Here is a simple example showing how to create a new directory called my_directory:
command = "mkdir my_directory" stdin, stdout, stderr = ssh_client.exec_command(command) print("Directory created:", ().decode())
In addition, you can also create directories through SFTP sessions. Paramiko provides an SFTP client that can easily transfer and directory management. Here is an example of creating a directory through SFTP:
sftp_client = ssh_client.open_sftp() try: remote_path = '/path/to/your/folder/subfolder' sftp_client.makedirs(remote_path) # Multi-level directories will be automatically created hereexcept IOError as e: print(f"Error creating remote directory: {e}") finally: sftp_client.close() ssh_client.close()
4. Upload files
To upload a file, you can use the put() method of Paramiko's SFTPClient class. This method accepts two parameters: the local file path and the remote file path. The following example uploads the local file /tmp/ to the /home/user/ directory of the remote server:
sftp_client = ssh_client.open_sftp() try: local_file_path = '/tmp/' remote_file_path = '/home/user/' sftp_client.put(local_file_path, remote_file_path) print('File uploaded successfully') except FileNotFoundError: print("Local file not found!") except PermissionError: print("No permission to upload the file!") except Exception as e: print("An error occurred:", str(e)) finally: sftp_client.close() ssh_client.close()
During the actual operation, various exceptions may be encountered, such as the file does not exist, permission problems, etc. Therefore, it is very important to handle exceptions reasonably.
5. Download the file
To download a file, you can use the get() method of Paramiko's SFTPClient class. This method accepts two parameters: the remote file path and the local file path. The following example downloads the /home/user/ file of the remote server to the local /tmp/ directory:
sftp_client = ssh_client.open_sftp() try: remote_file_path = '/home/user/' local_file_path = '/tmp/' sftp_client.get(remote_file_path, local_file_path) print('File downloaded successfully') except FileNotFoundError: print("Remote file not found!") except PermissionError: print("No permission to download the file!") except Exception as e: print("An error occurred:", str(e)) finally: sftp_client.close() ssh_client.close()
6. Upload and download folders
To upload a folder, you can use the put() method of Paramiko's SFTPClient class to upload files one by one, or you can use the put_recursive() method to upload a folder recursively. The following example recursively uploads the local folder /tmp/folder to the /home/user/ directory of the remote server:
import os def upload_folder(local_folder_path, remote_folder_path): sftp_client = ssh_client.open_sftp() try: for root, dirs, files in (local_folder_path): remote_current_path = remote_folder_path + root[len(local_folder_path):].strip() if not sftp_client.listdir(remote_current_path): sftp_client.makedirs(remote_current_path) for file in files: local_file_path = (root, file) remote_file_path = (remote_current_path, file) sftp_client.put(local_file_path, remote_file_path) print('Folder uploaded successfully') except Exception as e: print("An error occurred:", str(e)) finally: sftp_client.close() ssh_client.close() upload_folder('/tmp/folder', '/home/user/')
To download folders, you can use the get() method of Paramiko's SFTPClient class to download files one by one, or you can use the get_recursive() method to download folders recursively. The following example recursively downloads the /home/user/folder folder of the remote server to the local /tmp/ directory:
import os def download_folder(remote_folder_path, local_folder_path): sftp_client = ssh_client.open_sftp() try: if not (local_folder_path): (local_folder_path) for filename in sftp_client.listdir(remote_folder_path): remote_file_path = (remote_folder_path, filename) local_file_path = (local_folder_path, filename) if sftp_client.stat(remote_file_path).st_mode & 0o170000 == 0o040000: # If it is a folder download_folder(remote_file_path, local_file_path) else: sftp_client.get(remote_file_path, local_file_path) print('Folder downloaded successfully') except Exception as e: print("An error occurred:", str(e)) finally: sftp_client.close() ssh_client.close() download_folder('/home/user/folder', '/tmp/')
7. Exception handling and advanced functions
During the actual operation, various exceptions may be encountered, such as the file does not exist, permission problems, etc. Therefore, it is very important to handle exceptions reasonably. In addition, in some scenarios, when the network is unstable or the file is large, the breakpoint continuous transmission and error retry functions are particularly important.
You can achieve breakpoint retries by setting the resumable parameter of the put method to True (it should be noted that Paramiko itself does not directly support breakpoint retries, which is only a possible extension idea), and implement error retries through looping and exception handling.
Here is an example upload file with an error retry mechanism:
import time def upload_file_with_retry(local_file_path, remote_file_path, retries=3, delay=2): sftp_client = ssh_client.open_sftp() attempt = 0 while attempt < retries: try: sftp_client.put(local_file_path, remote_file_path) print('File uploaded successfully') return except Exception as e: print(f"Attempt {attempt + 1} failed: {str(e)}") attempt += 1 if attempt < retries: (delay) finally: sftp_client.close() ssh_client.close() print('Failed to upload file after retries') upload_file_with_retry('/tmp/', '/home/user/')
8. Summary
With the guidance of this article, you should now be able to use Python's Paramiko library to create remote file directories and upload files. These skills can not only improve your work efficiency, but also allow you to go further on the road of automated operations and maintenance. Remember to practice more in practice to better master these useful tools.
In addition, sensitive information such as server address, username and password should be kept properly and should not be disclosed in public.
This is the article about creating a file directory and uploading a file in detail. For more information about Python Paramiko creating a file directory, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!