import logging
from ceph_deploy.lib import remoto
from ceph_deploy.connection import get_local_connection


def can_connect_passwordless(hostname):
    """
    Ensure that current host can SSH remotely to the remote
    host using the ``BatchMode`` option to prevent a password prompt.

    That attempt will error with an exit status of 255 and a ``Permission
    denied`` message.
    """
    # Ensure we are not doing this for local hosts
    if not remoto.connection.needs_ssh(hostname):
        return True

    logger = logging.getLogger(hostname)
    with get_local_connection(logger) as conn:
        # Check to see if we can login, disabling password prompts
        command = ['ssh', '-CT', '-o', 'BatchMode=yes', hostname]
        out, err, retval = remoto.process.check(conn, command, stop_on_error=False)
        expected_error = 'Permission denied '
        has_key_error = False
        for line in err:
            if expected_error in line:
                has_key_error = True

        if retval == 255 and has_key_error:
            return False
    return True

@@ -366,16 +366,61 @@
 denied%60%60
+ message or a%60%60Host key verification failed%60%60
  message
@@ -864,22 +864,31 @@
         
-expect
+permission_deni
 ed_error
@@ -911,16 +911,80 @@
 enied '%0A
+        host_key_verify_error = 'Host key verification failed.'%0A
         
@@ -1049,16 +1049,58 @@
  if 
-expected
+permission_denied_error in line or host_key_verify
 _err

Check if ssh command prints "Host key verification failed." error message