Ginger_Chacha
Ginger_Chacha

Reputation: 347

"EOF in transport thread" when implementing challenge-response authentication with Python Paramiko

I need to login to a Linux server and it has below configuration in sshd_config:

PasswordAuthentication yes 
ChallengeResponseAuthentication yes 
UsePAM yes

When I login through PuTTY, it firstly asked me to input the password, followed by the RSA token from an authentication app. I need to do the same via Python, for some automation tasks. Here's my code:

import paramiko, traceback
from getpass import getpass
paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
    
hostname = '192.169.10.10'  
port = 22                      
username = get_user_name()
password = keyring.get_password('unix',username) # This is my first password

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
    transport = paramiko.Transport((hostname, port))
    try:
        transport.connect(username=username, password=password)
    except Exception as e:
        print(e) 
                 

    def challenge_handler(title, instructions, prompt_list):
        responses = []
        for prompt in prompt_list:
            if "password" in prompt[0].lower():
                responses.append(password)
            elif "rsa" in prompt[0].lower() :
                token = getpass(f"Enter {prompt[0].strip()}: ")
                responses.append(token)
            else:
                responses.append(getpass(f"Enter {prompt[0].strip()}: "))
        return responses

    transport.auth_interactive(username, handler=challenge_handler) #problem starts
    print("Authentication successful.") 

    session = transport.open_session(timeout=10) #Failed with EOF problem
    if session.active:
        print("Session opened successfully.")

        session.exec_command('uname')

        output = session.recv(1024).decode()
        print("Command output:")
        print(output)

        error = session.recv_stderr(1024).decode()
        if error:
            print("Command errors:")
            print(error)

        session.close()
    else:
        print("Failed to open session.")

except Exception as e:
    print(f"Error: {e}")
    traceback.print_exc()
finally:
    if 'ssh' in locals():
        ssh.close()
    print("Connection closed.")

I couldn't figure out what's wrong. Appreciate if you can shed some lights. If you feel i missed some information please let me know.

And I got below logs from paramiko before that Authentication successful log

INFO:paramiko.transport:Authentication (keyboard-interactive) successful! DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes DEBUG:paramiko.transport:EOF in transport thread i think this is where the problem starts

Thank you in advance

Upvotes: 0

Views: 37

Answers (1)

Ginger_Chacha
Ginger_Chacha

Reputation: 347

I got it now! Change

transport.connect(username=username, password=password)

To

transport.start_client()

resolve the problem. I should not try to connect. This is a ChallengeResponseAuthentication yes setup & my password is part of that process

Upvotes: 0

Related Questions