Paramiko provides several functions and classes that facilitate SSH communication and SFTP (Secure File Transfer Protocol) operations. Here are some key functions and classes along with brief explanations:
1. SSHClient
- connect: Establishes a connection to a remote SSH server using hostname, port, username, and password or private key.
- exec_command: Executes a command on the remote server. It returns three file-like objects representing stdin, stdout, and stderr.
- get_transport: Returns the underlying Transport object for the SSH connection, which can be used for more advanced operations.
- open_sftp: Opens an SFTP session over the SSH connection, allowing file operations like uploading, downloading, and listing directories.
- close: Closes the SSH connection.
2. SFTPClient
- get: Downloads a file from the remote server to the local machine.
- put: Uploads a file from the local machine to the remote server.
- listdir: Lists the contents of a directory on the remote server.
- remove: Deletes a file on the remote server.
- mkdir: Creates a new directory on the remote server.
- rmdir: Removes a directory on the remote server.
3. Transport
- open_session: Opens a new channel over the SSH connection for executing commands or data transfer.
- accept: Accepts an incoming connection request (used on the server side).
- start_server: Starts an SSH server using a provided handler for incoming requests.
4. Authentication Methods
- Paramiko supports various authentication methods, including password, public key, and agent-based authentication. The
connect
method inSSHClient
accepts parameters likepassword
,pkey
(for private key authentication), andlook_for_keys
(for automatic key lookup).
5. Exception Handling
- Paramiko defines several exceptions, such as
SSHException
,AuthenticationException
, andSFTPError
, to handle different error scenarios that may arise during SSH operations.
6. AutoAddPolicy
AutoAddPolicy
is a class provided by Paramiko that automatically adds the host key of the server to the HostKeys
object when connecting to an unknown host. This is typically used when the client doesn’t have the server’s host key stored, and you want to bypass the usual host key verification step. However, it’s important to note that using AutoAddPolicy
can expose you to security risks, such as man-in-the-middle attacks, because it trusts any host key.
7. exec_command
exec_command
is a method of the SSHClient
class that executes a command on a remote server via the SSH connection. It returns a tuple of three file-like objects: stdin
, stdout
, and stderr
. These objects correspond to the standard input, standard output, and standard error streams of the executed command.
Sample code
import paramiko
# Create an SSH client
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the remote server
client.connect('hostname', username='user', password='password')
# Execute a command
stdin, stdout, stderr = client.exec_command('ls -l')
print(stdout.read().decode())
# Transfer a file using SFTP
sftp = client.open_sftp()
sftp.get('/remote/path/file.txt', '/local/path/file.txt')
sftp.close()
# Close the SSH connection
client.close()