<commit_before># pyinfra
# File: pyinfra/modules/ssh.py
# Desc: use SSh from the remote server

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

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

from pyinfra.api import operation, OperationError


@operation
def keyscan(state, host, hostname, force=False):
    '''
    Manage the hosts in ``~/.ssh/known_hosts``.

    + hostname: hostname that should have a key in ``known_hosts``
    + force: if the key already exists, remove and rescan
    '''

    hostname_present = host.fact.find_in_file(
        '~/.ssh/known_hosts',
        hostname,
    )

    keyscan_command = 'ssh-keyscan {0} >> ~/.ssh/known_hosts'.format(hostname)<commit_msg>Ensure `~/.ssh` exists before SSH keyscanning.<commit_after># pyinfra
# File: pyinfra/modules/ssh.py
# Desc: use SSh from the remote server

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

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

from pyinfra.api import operation, OperationError

from . import files


@operation
def keyscan(state, host, hostname, force=False):
    '''
    Manage the hosts in ``~/.ssh/known_hosts``.

    + hostname: hostname that should have a key in ``known_hosts``
    + force: if the key already exists, remove and rescan
    '''

    yield files.directory(
        state, host,
        '~/.ssh',
        mode=700,
    )

    hostname_present = host.fact.find_in_file(
        '~/.ssh/known_hosts',
        hostname,
    )

    keyscan_command = 'ssh-keyscan {0} >> ~/.ssh/known_hosts'.format(hostname)
{'fraction_non_alphanumeric': 0.1345065996228787, 'fraction_numerical': 0.0031426775612822125, 'mean_word_length': 3.1894736842105265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2449369', 'n_tokens_mistral': 546, 'n_tokens_neox': 509, 'n_words': 145}