Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paramiko: password + authenticator app-passcode autentification #2074

Open
shoaib-intro opened this issue Jun 26, 2022 · 1 comment
Open
Labels

Comments

@shoaib-intro
Copy link

shoaib-intro commented Jun 26, 2022

Hi I'm trying to connect server which ask:
Password:
Passcode: coming from authenticator app

I tried following code:

def handler(title, instructions, prompt_list):
        answers = []
        for prompt_, _ in prompt_list:
            prompt = prompt_.strip().lower()
            if 'password' in prompt:
                password = getpass.getpass(prompt)
                answers.append(password)
            elif 'passcode' in prompt:
                otp = getpass.getpass(prompt)
                answers.append(otp)
            else:
                print('Unknown prompt: {prompt_}')
        return answers

transport = paramiko.Transport(host)
transport.start_client()
transport.auth_interactive(username, handler)

Error: INFO:paramiko.transport:Authentication (keyboard-interactive) failed.
It failed password authentication then I tried

def interactive_auth_handler(title, instructions, prompt_list):
    if prompt_list:
        if prompt_list[0][0]=="Password: ":
            return [password]
        return [getpass.getpass(prompt_list[0][0])]
    return []


client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    client.connect(host, port, username=username, password=password, pkey=None)
except paramiko.ssh_exception.SSHException as e:
    transport = client.get_transport()
    transport.auth_interactive(username, interactive_auth_handler)

but again authentication failed could you please tell me what actually problem is there. following is error from paramiko debug:

INFO:paramiko.transport:Authentication (password) failed.
@chrisinmtown
Copy link

chrisinmtown commented Jul 14, 2023

I believe the first code block sequence is appropriate: create a Transport, call start_client, call auth_interactive. The presence of (password) in the error message makes me question whether your custom handler is being invoked. I would first try adding print() or a loggers etc. to your interactive_auth_handler to see exactly what's going on. For comparison, in my code (example is posted to issue #1011) if I pass an invalid password to my own custom interactive authentication handler, I get error message paramiko.ssh_exception.AuthenticationException: Authentication failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants