Best Python code snippet using yandex-tank
ssh_known_hosts.py
Source:ssh_known_hosts.py  
1'''2Control of SSH known_hosts entries.3===================================4Manage the information stored in the known_hosts files5.. code-block:: yaml6    github.com:7      ssh_known_hosts:8        - present9        - user: root10        - fingerprint: 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:4811    example.com:12      ssh_known_hosts:13        - absent14        - user: root15'''16def present(17        name,18        user,19        fingerprint=None,20        port=None,21        enc=None,22        config='.ssh/known_hosts',23        hash_hostname=True):24    '''25    Verifies that the specified host is known by the specified user26    On many systems, specifically those running with openssh 4 or older, the27    ``enc`` option must be set, only openssh 5 and above can detect the key28    type.29    name30        The name of the remote host (e.g. "github.com")31    user32        The user who owns the ssh authorized keys file to modify33    enc34        Defines what type of key is being used, can be ecdsa ssh-rsa or ssh-dss35    fingerprint36        The fingerprint of the key which must be presented in the known_hosts37        file38    port39        optional parameter, denoting the port of the remote host, which will be40        used in case, if the public key will be requested from it. By default41        the port 22 is used.42    config43        The location of the authorized keys file relative to the user's home44        directory, defaults to ".ssh/known_hosts"45    hash_hostname : True46        Hash all hostnames and addresses in the output.47    '''48    ret = {'name': name,49           'changes': {},50           'result': None if __opts__['test'] else True,51           'comment': ''}52    if __opts__['test']:53        result = __salt__['ssh.check_known_host'](user, name,54                                                  fingerprint=fingerprint,55                                                  config=config)56        if result == 'exists':57            comment = 'Host {0} is already in {1}'.format(name, config)58            ret['result'] = True59            return dict(ret, comment=comment)60        elif result == 'add':61            comment = 'Key for {0} is set to be added to {1}'.format(name,62                                                                     config)63            return dict(ret, comment=comment)64        else:  # 'update'65            comment = 'Key for {0} is set to be updated in {1}'.format(name,66                                                                     config)67            return dict(ret, comment=comment)68    result = __salt__['ssh.set_known_host'](user, name,69                fingerprint=fingerprint,70                port=port,71                enc=enc,72                config=config,73                hash_hostname=hash_hostname)74    if result['status'] == 'exists':75        return dict(ret,76                    comment='{0} already exists in {1}'.format(name, config))77    elif result['status'] == 'error':78        return dict(ret, result=False, comment=result['error'])79    else:  # 'updated'80        fingerprint = result['new']['fingerprint']81        return dict(ret,82                changes={'old': result['old'], 'new': result['new']},83                comment='{0}\'s key saved to {1} (fingerprint: {2})'.format(84                         name, config, fingerprint))85def absent(name, user, config='.ssh/known_hosts'):86    '''87    Verifies that the specified host is not known by the given user88    name89        The host name90    user91        The user who owns the ssh authorized keys file to modify92    config93        The location of the authorized keys file relative to the user's home94        directory, defaults to ".ssh/known_hosts"95    '''96    ret = {'name': name,97           'changes': {},98           'result': None if __opts__['test'] else True,99           'comment': ''}100    known_host = __salt__['ssh.get_known_host'](user, name, config=config)101    if not known_host:102        return dict(ret, comment='Host is already absent')103    if __opts__['test']:104        comment = 'Key for {0} is set to be removed from {1}'.format(name,105                                                                     config)106        return dict(ret, comment=comment)107    rm_result = __salt__['ssh.rm_known_host'](user, name, config=config)108    if rm_result['status'] == 'error':109        return dict(ret, result=False, comment=rm_result['error'])110    else:111        return dict(ret,112                    changes={'old': known_host, 'new': None},113                    result=True,...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
