pyinfra.connectors.ssh module

Connect to hosts over SSH. This is the default connector and all targets default to this meaning you do not need to specify it - ie the following two commands are identical:

pyinfra ...
pyinfra @ssh/ ...
class pyinfra.connectors.ssh.Meta

Bases: pyinfra.api.connectors.BaseConnectorMeta

class DataKeys

Bases: object

allow_agent = 'Allow using SSH agent'
config_file = 'Custom SSH config file'
forward_agent = 'Enable SSH forward agent'
hostname = 'SSH hostname'
key = 'Key file to use for authentication'
key_password = 'Key file password'
known_hosts_file = 'Custom SSH known hosts file'
look_for_keys = 'Allow looking up users keys'
paramiko_connect_kwargs = "Override keyword arguments passed into paramiko's `SSHClient.connect`"
password = 'Password to use for authentication'
port = 'SSH port'
strict_host_key_checking = 'Override strict host keys check setting'
user = 'User to SSH as'
handles_execution = True
keys_prefix = 'ssh'
pyinfra.connectors.ssh.connect(state, host)

Connect to a single host. Returns the SSH client if succesful. Stateless by design so can be run in parallel.

pyinfra.connectors.ssh.get_file(state, host, remote_filename, filename_or_io, remote_temp_filename=None, sudo=False, sudo_user=None, su_user=None, print_output=False, print_input=False, **command_kwargs)

Download a file from the remote host using SFTP. Supports download files with sudo by copying to a temporary directory with read permissions, downloading and then removing the copy.

pyinfra.connectors.ssh.put_file(state, host, filename_or_io, remote_filename, remote_temp_filename=None, sudo=False, sudo_user=None, su_user=None, print_output=False, print_input=False, **command_kwargs)

Upload file-ios to the specified host using SFTP. Supports uploading files with sudo by uploading to a temporary directory then moving & chowning.

pyinfra.connectors.ssh.rsync(state, host, src, dest, flags, print_output=False, print_input=False, sudo=False, sudo_user=None, **ignored_kwargs)
pyinfra.connectors.ssh.run_shell_command(state, host, command, get_pty=False, timeout=None, stdin=None, success_exit_codes=None, print_output=False, print_input=False, return_combined_output=False, **command_kwargs)

Execute a command on the specified host.

  • state (pyinfra.api.State obj) – state object for this command
  • hostname (string) – hostname of the target
  • command (string) – actual command to execute
  • sudo (boolean) – whether to wrap the command with sudo
  • sudo_user (string) – user to sudo to
  • get_pty (boolean) – whether to get a PTY before executing the command
  • env (dict) – environment variables to set
  • timeout (int) – timeout for this command to complete before erroring

(exit_code, stdout, stderr) stdout and stderr are both lists of strings from each buffer.

Return type