Search code examples
python-3.xsshparamiko

Paramiko: password + sms-passcode autentification


I need to connect to a server using ssh from Python. Usually I use Paramiko, but this server requires a passcode that is sent on SMS after the password is entered:

C:\>ssh user@hostname.com
AD Password: <password>
Enter PASSCODE: <sms passcode>

Is it possible via Paramiko / Python


Solution

  • I do not know if this is the right way to do it, but it seems to work in my case:

    def interactive_auth_handler(title, instructions, prompt_list):
        if prompt_list:
            if prompt_list[0][0]=="AD 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)