airflow.providers.ssh.hooks.ssh¶
Hook for SSH connections.
Attributes¶
Classes¶
Execute remote commands with Paramiko. |
|
Asynchronous SSH hook using asyncssh for use in triggers. |
Functions¶
|
Module Contents¶
- class airflow.providers.ssh.hooks.ssh.SSHHook(ssh_conn_id=None, remote_host='', username=None, password=None, key_file=None, port=None, conn_timeout=None, cmd_timeout=NOTSET, keepalive_interval=30, banner_timeout=30.0, disabled_algorithms=None, ciphers=None, auth_timeout=None, host_proxy_cmd=None, conn_retry_attempts=3)[source]¶
Bases:
airflow.providers.common.compat.sdk.BaseHookExecute remote commands with Paramiko.
See also
This hook also lets you create ssh tunnel and serve as basis for SFTP file transfer.
- Parameters:
ssh_conn_id (str | None) – ssh connection id from airflow Connections from where all the required parameters can be fetched like username, password or key_file, though priority is given to the params passed during init.
remote_host (str) – remote host to connect
username (str | None) – username to connect to the remote_host
password (str | None) – password of the username to connect to the remote_host
key_file (str | None) – path to key file to use to connect to the remote_host
port (int | None) – port of remote host to connect (Default is paramiko SSH_PORT)
conn_timeout (int | None) – timeout (in seconds) for the attempt to connect to the remote_host. The default is 10 seconds. If provided, it will replace the conn_timeout which was predefined in the connection of ssh_conn_id.
cmd_timeout (float | airflow.sdk.definitions._internal.types.ArgNotSet | None) – timeout (in seconds) for executing the command. The default is 10 seconds. Nullable, None means no timeout. If provided, it will replace the cmd_timeout which was predefined in the connection of ssh_conn_id.
keepalive_interval (int) – send a keepalive packet to remote host every keepalive_interval seconds
banner_timeout (float) – timeout to wait for banner from the server in seconds
disabled_algorithms (dict | None) – dictionary mapping algorithm type to an iterable of algorithm identifiers, which will be disabled for the lifetime of the transport
ciphers (list[str] | None) – list of ciphers to use in order of preference
auth_timeout (int | None) – timeout (in seconds) for the attempt to authenticate with the remote_host
conn_retry_attempts (int) – number of times to attempt the initial SSH connection before giving up (default 3). Raising this helps when many tasks target the same SSH server at once and some connections are transiently refused (e.g.
sshdMaxStartupsthrottling).
- get_tunnel(remote_port, remote_host='localhost', local_port=None)[source]¶
Create a local port-forwarding tunnel through the SSH connection.
This is conceptually similar to
ssh -L <LOCAL_PORT>:<remote_host>:<REMOTE_PORT>.The returned
SSHTunnelshould be used as a context manager:with hook.get_tunnel(remote_port=5432) as tunnel: connect_to("localhost", tunnel.local_bind_port)
The
.start()/.stop()methods still work but are deprecated.Changed in version 4.4.0: Returns
SSHTunnelinstead ofsshtunnel.SSHTunnelForwarder. The tunnel now reuses the hook’s SSH connection (get_conn()) instead of establishing a separate one.- Parameters:
- Returns:
SSHTunnel instance
- Return type:
- class airflow.providers.ssh.hooks.ssh.SSHHookAsync(ssh_conn_id=default_conn_name, host=None, port=None, username=None, password=None, known_hosts=default_known_hosts, key_file='', passphrase='', private_key='', keepalive_interval=30)[source]¶
Bases:
airflow.providers.common.compat.sdk.BaseHookAsynchronous SSH hook using asyncssh for use in triggers.
This hook provides async SSH connectivity for deferrable operators and their triggers.
- Parameters:
ssh_conn_id (str) – SSH connection ID from Airflow Connections
host (str | None) – hostname of the SSH server (overrides connection)
port (int | None) – port of the SSH server (overrides connection)
username (str | None) – username for authentication (overrides connection)
password (str | None) – password for authentication (overrides connection)
known_hosts (str) – path to known_hosts file. Defaults to
~/.ssh/known_hosts.key_file (str) – path to private key file for authentication
passphrase (str) – passphrase for the private key
private_key (str) – private key content as string
- async get_conn()[source]¶
Open an asyncssh connection that can be reused for multiple commands.
Unlike
run_command(), the returned connection is not closed automatically; the caller owns its lifecycle (e.g.async with await hook.get_conn() as conn: ...or an explicitconn.close()). Reusing one connection avoids a new TCP/SSH handshake per command, which matters when many tasks poll the same SSH server.
- async run_command(command, timeout=None)[source]¶
Execute a command on the remote host asynchronously.
- async get_tunnel(remote_port, remote_host='localhost', local_port=None)[source]¶
Create an async local port-forwarding tunnel through the SSH connection.
Usage:
async with await hook.get_tunnel(remote_port=5432) as tunnel: connect_to("localhost", tunnel.local_bind_port)
- Parameters:
- Returns:
AsyncSSHTunnel instance
- Return type: