Ssh Operations

Execute commands and up/download files from the remote host.

Eg: pyinfra -> inventory-host.net <-> another-host.net

ssh.command

Execute commands on other servers over SSH.

ssh.command(hostname, command, user=None, port=22, ssh_user=None)
  • hostname: the hostname to connect to

  • command: the command to execute

  • user: connect with this user

  • port: connect to this port

Example:

ssh.command(
    name='Create file by running echo from host one to host two',
    hostname='two.example.com',
    command='echo "one was here" > /tmp/one.txt',
    user='vagrant',
)

ssh.download

Download files from other servers using scp.

ssh.download(
    hostname, filename, local_filename=None, force=False, port=22, user=None,
    ssh_keyscan=False, ssh_user=None,
)
  • hostname: hostname to upload to

  • filename: file to download

  • local_filename: where to download the file to (defaults to filename)

  • force: always download the file, even if present locally

  • port: connect to this port

  • user: connect with this user

  • ssh_keyscan: execute ssh.keyscan before uploading the file

ssh.keyscan

Check/add hosts to the ~/.ssh/known_hosts file.

ssh.keyscan(hostname, force=False, port=22)
  • hostname: hostname that should have a key in known_hosts

  • force: if the key already exists, remove and rescan

Example:

ssh.keyscan(
    name='Set add server two to known_hosts on one',
    hostname='two.example.com',
)

ssh.upload

Upload files to other servers using scp.

ssh.upload(
    hostname, filename, remote_filename=None, port=22, user=None, use_remote_sudo=False,
    ssh_keyscan=False, ssh_user=None,
)
  • hostname: hostname to upload to

  • filename: file to upload

  • remote_filename: where to upload the file to (defaults to filename)

  • port: connect to this port

  • user: connect with this user

  • use_remote_sudo: upload to a temporary location and move using sudo

  • ssh_keyscan: execute ssh.keyscan before uploading the file