import socket


def test_ssh(host, throw=False):
    """
    test ssh connection to specified `host`
    Useful utility to ensure if newly created instance is sshable
    """
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.settimeout(1)
        sock.connect((host, 22))
        return True
    except socket.timeout:
        if throw:
            raise
    except socket.error as e:
        if throw or e.errno != 111:
            raise
    finally:
        sock.close()
    return False