SSH client & key policies
Functions
hexlify | b2a_hex(data) -> s; Hexadecimal representation of binary data. |
retry_on_signal(function) | Retries function until it doesn’t raise an EINTR error |
Classes
Agent() | Client interface for using private keys from an SSH agent running on the local machine. |
AutoAddPolicy | Policy for automatically adding the hostname and new host key to the local .HostKeys object, and saving it. |
DSSKey([msg, data, filename, password, ...]) | Representation of a DSS key which can be used to sign an verify SSH2 data. |
HostKeys([filename]) | Representation of an OpenSSH-style “known hosts” file. |
MissingHostKeyPolicy | Interface for defining the policy that .SSHClient should use when the SSH server’s hostname is not in either the system host keys or the application’s keys. |
RSAKey([msg, data, filename, password, ...]) | Representation of an RSA key which can be used to sign and verify SSH2 data. |
RejectPolicy | Policy for automatically rejecting the unknown hostname & key. |
SSHClient() | A high-level representation of a session with an SSH server. |
Transport(sock[, default_window_size, ...]) | An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called channels <.Channel>, across the session. |
WarningPolicy | Policy for logging a Python-style warning for an unknown host key, but accepting it. |
Exceptions
BadHostKeyException(hostname, got_key, ...) | The host key given by the SSH server did not match what we were expecting. |
SSHException | Exception raised by failures in SSH2 protocol negotiation or logic errors. |
Policy for automatically adding the hostname and new host key to the local .HostKeys object, and saving it. This is used by .SSHClient.
Interface for defining the policy that .SSHClient should use when the SSH server’s hostname is not in either the system host keys or the application’s keys. Pre-made classes implement policies for automatically adding the key to the application’s .HostKeys object (.AutoAddPolicy), and for automatically rejecting the key (.RejectPolicy).
This function may be used to ask the user to verify the key, for example.
Called when an .SSHClient receives a server key for a server that isn’t in either the system or local .HostKeys object. To accept the key, simply return. To reject, raised an exception (which will be passed to the calling application).
Policy for automatically rejecting the unknown hostname & key. This is used by .SSHClient.
A high-level representation of a session with an SSH server. This class wraps .Transport, .Channel, and .SFTPClient to take care of most aspects of authenticating and opening channels. A typical use case is:
client = SSHClient()
client.load_system_host_keys()
client.connect('ssh.example.com')
stdin, stdout, stderr = client.exec_command('ls -l')
You may pass in explicit overrides for authentication and server host key checking. The default mechanism is to try to use local key files or an SSH agent (if one is running).
Instances of this class may be used as context managers.
New in version 1.6.
Create a new SSHClient.
Close this SSHClient and its underlying .Transport.
Connect to an SSH server and authenticate to it. The server’s host key is checked against the system host keys (see load_system_host_keys) and any local host keys (load_host_keys). If the server’s hostname is not found in either set of host keys, the missing host key policy is used (see set_missing_host_key_policy). The default policy is to reject the key and raise an .SSHException.
Authentication is attempted in the following order of priority:
- The pkey or key_filename passed in (if any)
- Any key we can find through an SSH agent
- Any “id_rsa”, “id_dsa” or “id_ecdsa” key discoverable in ~/.ssh/
- Plain username/password auth, if a password was given
If a private key requires a password to unlock it, and a password is passed in, that password will be used to attempt to unlock the key.
Parameters: |
|
---|---|
Raises: |
|
Changed in version 1.15: Added the banner_timeout, gss_auth, gss_kex, gss_deleg_creds and gss_host arguments.
Execute a command on the SSH server. A new .Channel is opened and the requested command is executed. The command’s input and output streams are returned as Python file-like objects representing stdin, stdout, and stderr.
Parameters: | |
---|---|
Returns: | the stdin, stdout, and stderr of the executing command, as a 3-tuple |
Raises SSHException: | |
if the server fails to execute the command |
Get the local .HostKeys object. This can be used to examine the local host keys or change them.
Returns: | the local host keys as a .HostKeys object. |
---|
Return the underlying .Transport object for this SSH connection. This can be used to perform lower-level tasks, like opening specific kinds of channels.
Returns: | the .Transport for this connection |
---|
Start an interactive shell session on the SSH server. A new .Channel is opened and connected to a pseudo-terminal using the requested terminal type and size.
Parameters: |
|
---|---|
Returns: | a new .Channel connected to the remote shell |
Raises SSHException: | |
if the server fails to invoke a shell |
Load host keys from a local host-key file. Host keys read with this method will be checked after keys loaded via load_system_host_keys, but will be saved back by save_host_keys (so they can be modified). The missing host key policy .AutoAddPolicy adds keys to this set and saves them, when connecting to a previously-unknown server.
This method can be called multiple times. Each new set of host keys will be merged with the existing set (new replacing old if there are conflicts). When automatically saving, the last hostname is used.
Parameters: | filename (str) – the filename to read |
---|---|
Raises IOError: | if the filename could not be read |
Load host keys from a system (read-only) file. Host keys read with this method will not be saved back by save_host_keys.
This method can be called multiple times. Each new set of host keys will be merged with the existing set (new replacing old if there are conflicts).
If filename is left as None, an attempt will be made to read keys from the user’s local “known hosts” file, as used by OpenSSH, and no exception will be raised if the file can’t be read. This is probably only useful on posix.
Parameters: | filename (str) – the filename to read, or None |
---|---|
Raises IOError: | if a filename was provided and the file could not be read |
Open an SFTP session on the SSH server.
Returns: | a new .SFTPClient session object |
---|
Save the host keys back to a file. Only the host keys loaded with load_host_keys (plus any added directly) will be saved – not any host keys loaded with load_system_host_keys.
Parameters: | filename (str) – the filename to save to |
---|---|
Raises IOError: | if the file could not be written |
Set the channel for logging. The default is "paramiko.transport" but it can be set to anything you want.
Parameters: | name (str) – new channel name for logging |
---|
Set the policy to use when connecting to a server that doesn’t have a host key in either the system or local .HostKeys objects. The default policy is to reject all unknown servers (using .RejectPolicy). You may substitute .AutoAddPolicy or write your own policy class.
Parameters: | policy (.MissingHostKeyPolicy) – the policy to use when receiving a host key from a previously-unknown server |
---|
Policy for logging a Python-style warning for an unknown host key, but accepting it. This is used by .SSHClient.