def authorized_keys(name): """ Get the list of authorized SSH public keys for the user """ ssh_dir = posixpath.join(home_directory(name), '.ssh') authorized_keys_filename = posixpath.join(ssh_dir, 'authorized_keys') return uncommented_lines(authorized_keys_filename, use_sudo=True)
def add_host_keys(name, hostname): """ Add all public keys of a host to the user's SSH known hosts file """ from fabtools.require.files import ( directory as _require_directory, file as _require_file, ) ssh_dir = posixpath.join(home_directory(name), '.ssh') _require_directory(ssh_dir, mode='700', owner=name, use_sudo=True) known_hosts_filename = posixpath.join(ssh_dir, 'known_hosts') _require_file(known_hosts_filename, mode='644', owner=name, use_sudo=True) known_hosts = uncommented_lines(known_hosts_filename, use_sudo=True) with hide('running', 'stdout'): res = run('ssh-keyscan -t rsa,dsa %s 2>/dev/null' % hostname) for host_key in res.splitlines(): if host_key not in known_hosts: sudo('echo %s >>%s' % (quote(host_key), quote(known_hosts_filename)))